Python代码调试的几种方法总结

论坛 期权论坛 脚本     
niminba   2021-5-23 03:28   1734   0

使用 pdb 进行调试

pdb 是 python 自带的一个包,为 python 程序提供了一种交互的源代码调试功能,主要特性包括设置断点、单步调试、进入函数调试、查看当前代码、查看栈片段、动态改变变量的值等。pdb 提供了一些常用的调试命令,详情见表 1。
表 1. pdb 常用命令

2015415163749686.jpg (832×326)

下面结合具体的实例讲述如何使用 pdb 进行调试。
清单 1. 测试代码示例

import pdb 
 a = "aaa"
 pdb.set_trace() 
 b = "bbb"
 c = "ccc"
 final = a + b + c 
 print final

开始调试:直接运行脚本,会停留在 pdb.set_trace() 处,选择 n+enter 可以执行当前的 statement。在第一次按下了 n+enter 之后可以直接按 enter 表示重复执行上一条 debug 命令。
清单 2. 利用 pdb 调试

[root@rcc-pok-idg-2255 ~]# python epdb1.py 
 > /root/epdb1.py(4)?() 
 -> b = "bbb"
 (Pdb) n 
 > /root/epdb1.py(5)?() 
 -> c = "ccc"
 (Pdb) 
 > /root/epdb1.py(6)?() 
 -> final = a + b + c 
 (Pdb) list 
 1   import pdb 
 2   a = "aaa"
 3   pdb.set_trace() 
 4   b = "bbb"
 5   c = "ccc"
 6 -> final = a + b + c 
 7   print final 
 [EOF] 
 (Pdb) 
 [EOF] 
 (Pdb) n 
 > /root/epdb1.py(7)?() 
 -> print final 
 (Pdb)

退出 debug:使用 quit 或者 q 可以退出当前的 debug,但是 quit 会以一种非常粗鲁的方式退出程序,其结果是直接 crash。
清单 3. 退出 debug

[root@rcc-pok-idg-2255 ~]# python epdb1.py 
 > /root/epdb1.py(4)?() 
 -> b = "bbb"
 (Pdb) n 
 > /root/epdb1.py(5)?() 
 -> c = "ccc"
 (Pdb) q 
 Traceback (most recent call last): 
 File "epdb1.py", line 5, in ? 
  c = "ccc"
 File "epdb1.py", line 5, in ? 
  c = "ccc"
 File "/usr/lib64/python2.4/bdb.py", line 48, in trace_dispatch 
  return self.dispatch_line(frame) 
 File "/usr/lib64/python2.4/bdb.py", line 67, in dispatch_line 
  if self.quitting: raise BdbQuit 
 bdb.BdbQuit

打印变量的值:如果需要在调试过程中打印变量的值,可以直接使用 p 加上变量名,但是需要注意的是打印仅仅在当前的 statement 已经被执行了之后才能看到具体的值,否则会报 NameError: < exceptions.NameError … ....> 错误。
清单 4. debug 过程中打印变量

[root@rcc-pok-idg-2255 ~]# python epdb1.py 
 > /root/epdb1.py(4)?() 
 -> b = "bbb"
 (Pdb) n 
 > /root/epdb1.py(5)?() 
 -> c = "ccc"
 (Pdb) p b 
'bbb'
 (Pdb) 
'bbb'
 (Pdb) n 
 > /root/epdb1.py(6)?() 
 -> final = a + b + c 
 (Pdb) p c 
'ccc'
 (Pdb) p final 
 *** NameError: <exceptions.NameError instance at 0x1551b710 > 
 (Pdb) n 
 > /root/epdb1.py(7)?() 
 -> print final 
 (Pdb) p final 
'aaabbbccc'
 (Pdb)

使用 c 可以停止当前的 debug 使程序继续执行。如果在下面的程序中继续有 set_statement() 的申明,则又会重新进入到 debug 的状态,读者可以在代码 print final 之前再加上 set_trace() 验证。
清单 5. 停止 debug 继续执行程序

[root@rcc-pok-idg-2255 ~]# python epdb1.py 
 > /root/epdb1.py(4)?() 
 -> b = "bbb"
 (Pdb) n 
 > /root/epdb1.py(5)?() 
 -> c = "ccc"
 (Pdb) c 
 aaabbbccc

显示代码:在 debug 的时候不一定能记住当前的代码块,如要要查看具体的代码块,则可以通过使用 list 或者 l 命令显示。list 会用箭头 -> 指向当前 debug 的语句。
清单 6. debug 过程中显示代码

[root@rcc-pok-idg-2255 ~]# python epdb1.py 
 > /root/epdb1.py(4)?() 
 -> b = "bbb"
 (Pdb) list 
 1   import pdb 
 2   a = "aaa"
 3   pdb.set_trace() 
 4 -> b = "bbb"
 5   c = "ccc"
 6   final = a + b + c 
 7   pdb.set_trace() 
 8   print final 
 [EOF] 
 (Pdb) c 
 > /root/epdb1.py(8)?() 
 -> print final 
 (Pdb) list 
 3   pdb.set_trace() 
 4   b = "bbb"
 5   c = "ccc"
 6   final = a + b + c 
 7   pdb.set_trace() 
 8 -> print final 
 [EOF] 
 (Pdb)

在使用函数的情况下进行 debug
清单 7. 使用函数的例子

import pdb 
 def combine(s1,s2):   # define subroutine combine, which... 
  s3 = s1 + s2 + s1  # sandwiches s2 between copies of s1, ... 
  s3 = '"' + s3 +'"'  # encloses it in double quotes,... 
  return s3      # and returns it. 
 a = "aaa"
 pdb.set_trace() 
 b = "bbb"
 c = "ccc"
 final = combine(a,b) 
 print final

如果直接使用 n 进行 debug 则到 final=combine(a,b) 这句的时候会将其当做普通的赋值语句处理,进入到 print final。如果想要对函数进行 debug 如何处理呢 ? 可以直接使用 s 进入函数块。函数里面的单步调试与上面的tical info for log1:not worked

logging 的使用非常简单,同时它是线程安全的,下面结合多线程的例子讲述如何使用 logging 进行 debug。
清单 14. 多线程使用 logging

logging.conf 
 [loggers] 
 keys=root,simpleExample 

 [handlers] 
 keys=consoleHandler 

 [formatters] 
 keys=simpleFormatter 

 [logger_root] 
 level=DEBUG 
 handlers=consoleHandler 

 [logger_simpleExample] 
 level=DEBUG 
 handlers=consoleHandler 
 qualname=simpleExample 
 propagate=0 

 [handler_consoleHandler] 
 class=StreamHandler 
 level=DEBUG 
 formatter=simpleFormatter 
 args=(sys.stdout,) 

 [formatter_simpleFormatter] 
 format=%(asctime)s - %(name)s - %(levelname)s - %(message)s 
 datefmt= 

 code example: 
 #!/usr/bin/python 
 import thread 
 import time 
 import logging 
 import logging.config 
 logging.config.fileConfig('logging.conf') 
 # create logger 
 logger = logging.getLogger('simpleExample') 
 # Define a function for the thread 
 def print_time( threadName, delay): 
 logger.debug('thread 1 call print_time function body') 
 count = 0 
 logger.debug('count:%s',count)

总结

全文介绍了 python 中 debug 的几种不同的方式,包括 pdb 模块、利用 PyDev 和 Eclipse 集成进行调试、PyCharm 以及 Debug 日志进行调试,希望能给相关 python 使用者一点参考。更多关于 python debugger 的资料可以参见参考资料。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP