python3.7如何调试

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

python3.7如何调试

PyCharm IDE 窗口布局

python3.7如何调试

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>0andvalueB>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 栏中。当调试进行到该变量所在的语句时,在该窗口中可以直接看到该变量的具体值。