先举例最为常见的调试方法是:print
def foo(s): n = int(s) print('>>> n = %d' % n) return 10 / n def main(): foo('0') main()
运行结果:
>>> n = 0 Traceback (most recent call last): File "/usercode/file.py", line 13, inmain() File "/usercode/file.py", line 10, in main foo('0') File "/usercode/file.py", line 7, in foo return 10 / n ZeroDivisionError: integer division or modulo by zero
有时候并不想要print输出信息,只是为了方便看调试值而已,所以,assert可以解决这个问题。
assert(断言):
将print替换
当n != 0 为False时,才会执行'n is zero!'
def foo(s): n = int(s) assert n != 0, 'n is zero!' return 10 / ndef main(): foo('0')
运行结果:
Traceback (most recent call last): File "/usercode/file.py", line 14, inmain() File "/usercode/file.py", line 11, in main foo('0') File "/usercode/file.py", line 6, in foo assert n != 0, 'n is zero!' AssertionError: n is zero!
logging:
用logging代替print, 可将信息输出到文件中,但我还不懂是哪个文件,怎么找。。。。
logging.info()可以输出一段文本,但并不会主动将文本信息显示在会话框,所以需要加:
logging.basicConfig(level=logging.INFO)
没有在会话框输出logging内容的程序:
import logging s = '0' n = int(s) logging.info('n = %d' % n) print(10 / n)
运行结果:
Traceback (most recent call last): File "log_info.py", line 6, inprint(10 / n) ZeroDivisionError: integer division or modulo by zero
在会话框输出logging内容的程序:
import logging logging.basicConfig(level=logging.INFO) #####增加这句 s = '0' n = int(s) logging.info('n = %d' % n) print(10 / n)
运行结果:
INFO:root:n = 0 Traceback (most recent call last): File "log_dug1.py", line 7, inprint(10 / n) ZeroDivisionError: integer division or modulo by zero
看,可以把文本内容输出到了会话窗口了
INFO:root:n = 0
单步运行:pdb
命令:
进入调试状态:python -m pdb 文件名
查看文件内容:l(小写)
查看变量 :p 变量名
调试当前语句:n
退出调试 :q
s = '0' n = int(s) j = 10 / n d = j + 1 print('d', d)
运行结果:
z@g:~/Documents/Temp/ErrorDebug$ python -m pdb log_pdb.py > /home/seeing-zynq/Documents/Temp/ErrorDebug/log_pdb.py(4)() -> s = '0' (Pdb) l 1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 4 -> s = '0' 5 n = int(s) 6 j = 10 / n 7 d = j + 1 8 print('d', d) 9 10 11 (Pdb) n > /home/seeing-zynq/Documents/Temp/ErrorDebug/log_pdb.py(5) () -> n = int(s) (Pdb) n > /home/seeing-zynq/Documents/Temp/ErrorDebug/log_pdb.py(6) () -> j = 10 / n (Pdb) n ZeroDivisionError: 'integer division or modulo by zero' > /home/seeing-zynq/Documents/Temp/ErrorDebug/log_pdb.py(6) () -> j = 10 / n
当运行到了
-> j = 10 / n
就会提出错误信息
ZeroDivisionError: 'integer division or modulo by zero'
pdb虽好,但从头到尾开始,有点蛋疼,如果能在指定的地方开始测试就好了,所以,pdb.set_trace()在群众的欢呼中出来了。
#!/usr/bin/python # -*- coding: utf-8 -*- import pdb s = '0' n = int(s) pdb.set_trace() # 运行到这里会自动暂停 print(10 / n)
运行结果:直接运行.py文件
q@s:~/Documents/Temp/ErrorDebug$ python set_trace.py > /home/seeing-zynq/Documents/Temp/ErrorDebug/set_trace.py(9)() -> print(10 / n) (Pdb) p n --------------》查看变量n的值 0 (Pdb) p s '0' (Pdb) n ------------------》单步执行 ZeroDivisionError: 'integer division or modulo by zero' > /home/seeing-zynq/Documents/Temp/ErrorDebug/set_trace.py(9) () -> print(10 / n) (Pdb) c --------------------》自动往下执行 Traceback (most recent call last): File "set_trace.py", line 9, in print(10 / n) ZeroDivisionError: integer division or modulo by zero