当前位置: 首页 > 图灵资讯 > 行业资讯> python3.7如何调试

python3.7如何调试

来源:图灵python
时间: 2025-02-05 20:42:51

python3.7调试方法:首先将光标移动到需要设置断点调试的线路上;然后双击代码编辑器左侧边缘,出现小红点;然后点击主窗口右上角的绿色昆虫图标,在窗口左下角查看调试控制台,点击控制台上方的箭头逐步调试。

PyCharm IDE 窗口布局

1.png

PyCharm 调试代码实例(以我自己的代码为例)

__author__='lxm'
#!/usr/bin/python
importthread
importtime
#Defineafunctionforthethread
defprint_time(threadName,delay):
count=0
whilecount<5:
count+=1
print"%s:%s"%(threadName,time.ctime(time.time()))
defcheck_sum(threadName,valueA,valueB):
print"tocalculatethesumoftwonumberher"
result=sum(valueA,valueB)
print"theresultis",result;
defsum(valueA,valueB):
ifvalueA>0andvalueBe>0:
returnvalueA+valueB
defreadFile(threadName,filename):
file=open(filename)
forlineinfile.xreadlines():
printline
try:
thread.start_new_thread(print_time,("Thread-1",2,))
thread.start_new_thread(check_sum,("Thread-2",4,5,))
thread.start_new_thread(readFile,("Thread-3","test.txt",))
except:
print"Error:unabletostartthread"
while1:
#print"end"
pass

调试前通常需要设置断点,断点可以设置在循环或条件判断的表达或程序的关键点。设置断点的方法非常简单:将光标移动到需要设置断点的代码编辑框中,然后直接按下 Ctrl+F8 或者选择菜单"Run"->"Toggle Line Break Point",更直接的方法是双击代码编辑器左侧的边缘,看到红点。当调试开始时,当前执行的代码将直接显示为蓝色。下图中设置了三个断点,蓝色显示了正在执行的代码。

断点设置

表达式求值:在调试过程中,有时需要跟踪一些表达式值来发现程序中的问题,Pycharm 通过选择表达式,支持表达式求值,然后选择“Run”->”Evaluate Expression",直接选择出现的窗口 Evaluate 便可查看。

Pycharm同时提供 Variables 和 Watches 调试步骤中涉及的具体变量值可直接在窗口中 variable 在一栏中查看。

变量查看

若要动态监测某个变量,可直接选择该变量并选择菜单”Run”->”Add Watch”添加到 watches 栏中。当调试到变量所在的句子时,可以直接在窗口中看到变量的具体值。

推荐课程:Python 基本入门教程