使用 PyCharm 进行调试

    科技2022-08-08  110

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

     

    使用 PyCharm 进行调试

    前言一、PyCharm 是什么?二、调试步骤 1. 调试示例代码2. 设置断点3. evaluate expression表达式4. 多线程调试总结

     


    前言

    初步学习Python语言,需要对工具的使用熟悉,通常使用pycharm来进行代码调试,这里只要讲述一些关于该工具如何进行代码调试的步骤和方法。


     

    一、PyCharm 是什么?

    PyCharm 是由 JetBrains 打造的一款 Python IDE,具有语法高亮、Project 管理、代码跳转、智能提示、自动完成、单元测试、版本控制等功能,同时提供了对 Django 开发以及 Google App Engine 的支持。分为个人独立版和商业版,需要 license 支持,也可以获取免费的 30 天试用。试用版本的 Pycharm 可以在官网上下载,下载地址为:http://www.jetbrains.com/pycharm/download/index.html。 PyCharm 同时提供了较为完善的调试功能,支持多线程,远程调试等,可以支持断点设置,单步模式,表达式求值,变量查看等一系列功能。PyCharm IDE 的调试窗口布局如图 1 所示

    二、使用步骤

    1. 写入调试示例代码

    __author__ = 'lihong' # !/usr/bin/python import _thread import time # Define a function for the thread def print_time(thread_name, delay): count = 0 while count < 5: count += 1 print("%s: %s" % (thread_name, time.ctime(time.time()))) print("delay %s" % delay) def check_sum(thread_name, first_value, second_value): print("to calculate the sum of two number here") result = sum(first_value, second_value) print("thread_name %s" % thread_name) print("the result is", result) def sum(first_value, second_value): if first_value > 0 and second_value > 0: return first_value + second_value def read_file(thread_name, filename): file = open(filename, 'rb') for line in file.readlines(): print(line) print("thread_name %s", thread_name) try: _thread.start_new_thread(print_time, ("Thread-1", 2,)) _thread.start_new_thread(check_sum, ("Thread-2", 4, 5)) _thread.start_new_thread(read_file, ("Thread-3", "D:\\git\\test.txt",)) except: print("Error: unable to start thread") while 1: # print "end" pass

    2. 设置断点

    3. evaluate expression表达式

    4. 多线程调试


    总结

    以上就主要内容是关于pycharm工具用于代码调试的步骤,便于在学习Python的过程中方便调试代码。

    Processed: 0.013, SQL: 8