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

使用 pdb 进行调试php

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

2015415163749686.jpg (832×326)

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

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 继续执行程序app

1eclipse

2函数

3工具

4

5

6

7

8

[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 过程当中显示代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

[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. 使用函数的例子

?

1

2

3

4

5

6

7

8

9

10

11

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 进入函数块。函数里面的单步调试与上面的介绍相似。若是不想在函数里单步调试能够在断点处直接按 r 退出到调用的地方。
清单 8. 对函数进行 debug

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

[root@rcc-pok-idg-2255 ~]# python epdb2.py

 > /root/epdb2.py(10)?()

 -> b = "bbb"

 (Pdb) n

 > /root/epdb2.py(11)?()

 -> c = "ccc"

 (Pdb) n

 > /root/epdb2.py(12)?()

 -> final = combine(a,b)

 (Pdb) s

 --Call--

 > /root/epdb2.py(3)combine()

 -> def combine(s1,s2):   # define subroutine combine, which...

 (Pdb) n

 > /root/epdb2.py(4)combine()

 -> s3 = s1 + s2 + s1  # sandwiches s2 between copies of s1, ...

 (Pdb) list

 1   import pdb

 2

 3   def combine(s1,s2):   # define subroutine combine, which...

 4 ->   s3 = s1 + s2 + s1  # sandwiches s2 between copies of s1, ...

 5     s3 = '"' + s3 +'"'  # encloses it in double quotes,...

 6     return s3      # and returns it.

 7

 8   a = "aaa"

 9   pdb.set_trace()

 10   b = "bbb"

 11   c = "ccc"

 (Pdb) n

 > /root/epdb2.py(5)combine()

 -> s3 = '"' + s3 +'"'  # encloses it in double quotes,...

 (Pdb) n

 > /root/epdb2.py(6)combine()

 -> return s3      # and returns it.

 (Pdb) n

 --Return--

 > /root/epdb2.py(6)combine()->'"aaabbbaaa"'

 -> return s3      # and returns it.

 (Pdb) n

 > /root/epdb2.py(13)?()

 -> print final

 (Pdb)

在调试的时候动态改变值 。在调试的时候能够动态改变变量的值,具体以下实例。须要注意的是下面有个错误,缘由是 b 已经被赋值了,若是想从新改变 b 的赋值,则应该使用! B。
清单 9. 在调试的时候动态改变值

?

1

2

3

4

5

6

7

8

9

[root@rcc-pok-idg-2255 ~]# python epdb2.py

 > /root/epdb2.py(10)?()

 -> b = "bbb"

 (Pdb) var = "1234"

 (Pdb) b = "avfe"

 *** The specified object '= "avfe"' is not a function

 or was not found along sys.path.

 (Pdb) !b="afdfd"

 (Pdb)

pdb 调试有个明显的缺陷就是对于多线程,远程调试等支持得不够好,同时没有较为直观的界面显示,不太适合大型的 python 项目。而在较大的 python 项目中,这些调试需求比较常见,所以须要使用更为高级的调试工具。接下来将介绍 PyCharm IDE 的调试方法 .
使用 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. PyCharm IDE 窗口布局

2015415163831052.gif (568×364)

 

蜗牛学院Java培训100%高薪就业

【点击进入】

0基础入学,两周免费试学,100%就业,薪资 4k-6k,学费与工资挂钩!专家授课,定向培训!

查 看

 

下面结合实例讲述如何利用 PyCharm 进行多线程调试。具体调试所用的代码实例见清单 10。
清单 10. PyCharm 调试代码实例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

__author__ = 'zhangying'

 #!/usr/bin/python

 import thread

 import time

 # Define a function for the thread

 def print_time( threadName, delay):

 count = 0

 while count < 5:

  count += 1

  print "%s: %s" % ( threadName, time.ctime(time.time()) )

 def check_sum(threadName,valueA,valueB):

 print "to calculate the sum of two number her"

 result=sum(valueA,valueB)

 print "the result is" ,result;

 def sum(valueA,valueB):

 if valueA >0 and valueB>0:

  return valueA+valueB

 def readFile(threadName, filename):

 file = open(filename)

 for line in file.xreadlines():

  print line

 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: unable to start thread"

 while 1:

 # print "end"

 pass

在调试以前一般须要设置断点,断点能够设置在循环或者条件判断的表达式处或者程序的关键点。设置断点的方法很是简单:在代码编辑框中将光标移动到须要设置断点的行,而后直接按 Ctrl+F8 或者选择菜单"Run"->"Toggle Line Break Point",更为直接的方法是双击代码编辑处左侧边缘,能够看到出现红色的小圆点(如图 2)。当调试开始的时候,当前正在执行的代码会直接显示为蓝色。下图中设置了三个断点,蓝色高亮显示的为正在执行的代码。
图 2. 断点设置

2015415163858070.jpg (555×204)

 

蜗牛学院Java培训100%高薪就业

【点击进入】

0基础入学,两周免费试学,100%就业,薪资 4k-6k,学费与工资挂钩!专家授课,定向培训!

查 看

 

表达式求值:在调试过程当中有的时候须要追踪一些表达式的值来发现程序中的问题,Pycharm 支持表达式求值,能够经过选中该表达式,而后选择“Run”->”Evaluate Expression”,在出现的窗口中直接选择 Evaluate 即可以查看。

Pychar 同时提供了 Variables 和 Watches 窗口,其中调试步骤中所涉及的具体变量的值能够直接在 variable 一栏中查看。
图 3. 变量查看

2015415163926095.jpg (423×115)

若是要动态的监测某个变量能够直接选中该变量并选择菜单”Run”->”Add Watch”添加到 watches 栏中。当调试进行到该变量所在的语句时,在该窗口中能够直接看到该变量的具体值。
图 4. 监测变量

2015415163951352.jpg (278×178)

对于多线程程序来讲,一般会有多个线程,当须要 debug 的断点分别设置在不一样线程对应的线程体中的时候,一般须要 IDE 有良好的多线程调试功能的支持。 Pycharm 中在主线程启动子线程的时候会自动产生一个 Dummy 开头的名字的虚拟线程,每个 frame 对应各自的调试帧。如图 5,本实例中一共有四个线程,其中主线程生成了三个线程,分别为 Dummy-4,Dummy-5,Dummy-6. 其中 Dummy-4 对应线程 1,其他分别对应线程 2 和线程 3。
图 5. 多线程窗口

2015415164022485.jpg (306×252)

 

建行柜员招聘

建行柜员招聘

p2p网贷排名

p2p网贷排名

牙周炎怎么治疗

牙周炎怎么治疗

 

当调试进入到各个线程的子程序时,Frame 会自动切换到其所对应的 frame,相应的变量栏中也会显示与该过程对应的相关变量,如图 6,直接控制调试按钮,如 setp in,step over 即可以方便的进行调试。
图 6. 子线程调试

2015415164053690.jpg (568×410)

使用 PyDev 进行调试

PyDev 是一个开源的的 plugin,它能够方便的和 Eclipse 集成,提供方便强大的调试功能。同时做为一个优秀的 Python IDE 还提供语法错误提示、源代码编辑助手、Quick Outline、Globals Browser、Hierarchy View、运行等强大功能。下面讲述如何将 PyDev 和 Eclipse 集成。在安装 PyDev 以前,须要先安装 Java 1.4 或更高版本、Eclipse 以及 Python。 第一步:启动 Eclipse,在 Eclipse 菜单栏中找到 Help 栏,选择 Help > Install New Software,并选择 Add button,添加 Ptdev 的下载站点 http://pydev.org/updates。选择 PyDev 以后完成余下的步骤即可以安装 PyDev。
图 7. 安装 PyDev

2015415164129350.jpg (561×453)

安装完成以后须要配置 Python 解释器,在 Eclipse 菜单栏中,选择 Window > Preferences > Pydev > Interpreter – Python。Python 安装在 C:\Python27 路径下。单击 New,选择 Python 解释器 python.exe,打开后显示出一个包含不少复选框的窗口,选择须要加入系统 PYTHONPATH 的路径,单击 OK。
图 8. 配置 PyDev

2015415164223851.jpg (565×507)

在配置完 Pydev 以后,能够经过在 Eclipse 菜单栏中,选择 File > New > Project > Pydev >Pydev Project,单击 Next 建立 Python 项目,下面的内容假设 python 项目已经建立,而且有个须要调试的脚本 remote.py(具体内容以下),它是一个登录到远程机器上去执行一些命令的脚本,在运行的时候须要传入一些参数,下面将详细讲述如何在调试过程当中传入参数 .
清单 11. Pydev 调试示例代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

#!/usr/bin/env python

import os

def telnetdo(HOST=None, USER=None, PASS=None, COMMAND=None): #define a function

import telnetlib, sys

if not HOST:

try:

HOST = sys.argv[1]

USER = sys.argv[2]

PASS = sys.argv[3]

COMMAND = sys.argv[4]

except:

print "Usage: remote.py host user pass command"

return

tn = telnetlib.Telnet() #

try:

tn.open(HOST)

except:

print "Cannot open host"

return

tn.read_until("login:")

tn.write(USER + '\n')

if PASS:

tn.read_until("Password:")

tn.write(PASS + '\n')

tn.write(COMMAND + '\n')

tn.write("exit\n")

tmp = tn.read_all()

tn.close()

del tn

return tmp

 

if __name__ == '__main__':

print telnetdo()

在调试的时候有些状况须要传入一些参数,在调试以前须要进行相应的配置以便接收所须要的参数,选择须要调试的程序(本例 remote.py),该脚本在 debug 的过程当中须要输入四个参数:host,user,password 以及命令。在 eclipse 的工程目录下选择须要 debug 的程序,单击右键,选择“Debug As”->“Debug Configurations”,在 Arguments Tab 页中选择“Variables”。以下 图 9 所示 .
图 9. 配置变量

2015415164245562.gif (551×447)

在窗口”Select Variable”以后选择“Edit Varuables” ,出现以下窗口,在下图中选择”New” 并在弹出的窗口中输入对应的变量名和值。特别须要注意的是在值的后面必定要有空格,否则全部的参数都会被当作第一个参数读入。
图 10. 添加具体变量

2015415164327781.gif (524×300)

按照以上方式依次配置完全部参数,而后在”select variable“窗口中安装参数所须要的顺序依次选择对应的变量。配置完成以后状态以下图 11 所示。
图 11. 完成配置

2015415164403539.jpg (469×159)

选择 Debug 即可以开始程序的调试,调试方法与 eclipse 内置的调试功能的使用类似,而且支持多线程的 debug,这方面的文章已经有不少,读者能够自行搜索阅读,或者参考”使用 Eclipse 平台进行调试“一文。
使用日志功能达到调试的目的

日志信息是软件开发过程当中进行调试的一种很是有用的方式,特别是在大型软件开发过程须要不少相关人员进行协做的状况下。开发人员经过在代码中加入一些特定的可以记录软件运行过程当中的各类事件信息可以有利于甄别代码中存在的问题。这些信息可能包括时间,描述信息以及错误或者异常发生时候的特定上下文信息。 最原始的 debug 方法是经过在代码中嵌入 print 语句,经过输出一些相关的信息来定位程序的问题。但这种方法有必定的缺陷,正常的程序输出和 debug 信息混合在一块儿,给分析带来必定困难,当程序调试结束再也不须要 debug 输出的时候,一般没有很简单的方法将 print 的信息屏蔽掉或者定位到文件。python 中自带的 logging 模块能够比较方便的解决这些问题,它提供日志功能,将 logger 的 level 分为五个级别,能够经过 Logger.setLevel(lvl) 来设置。默认的级别为 warning。
表 2. 日志的级别

2015415164438205.jpg (811×197)

ogging lib 包含 4 个主要对象

  1.     logger:logger 是程序信息输出的接口。它分散在不一样的代码中使得程序能够在运行的时候记录相应的信息,并根据设置的日志级别或 filter 来决定哪些信息须要输出并将这些信息分发到其关联的 handler。经常使用的方法有 Logger.setLevel(),Logger.addHandler() ,Logger.removeHandler() ,Logger.addFilter() ,Logger.debug(), Logger.info(), Logger.warning(), Logger.error(),getLogger() 等。logger 支持层次继承关系,子 logger 的名称一般是父 logger.name 的方式。若是不建立 logger 的实例,则使用默认的 root logger,经过 logging.getLogger() 或者 logging.getLogger("") 获得 root logger 实例。
  2.     Handler:Handler 用来处理信息的输出,能够将信息输出到控制台,文件或者网络。能够经过 Logger.addHandler() 来给 logger 对象添加 handler,经常使用的 handler 有 StreamHandler 和 FileHandler 类。StreamHandler 发送错误信息到流,而 FileHandler 类用于向文件输出日志信息,这两个 handler 定义在 logging 的核心模块中。其余的 hander 定义在 logging.handles 模块中,如 HTTPHandler,SocketHandler。
  3.     Formatter:Formatter 则决定了 log 信息的格式 , 格式使用相似于 %(< dictionary key >)s 的形式来定义,如'%(asctime)s - %(levelname)s - %(message)s',支持的 key 能够在 python 自带的文档 LogRecord attributes 中查看。
  4.     Filter:Filter 用来决定哪些信息须要输出。能够被 handler 和 logger 使用,支持层次关系,好比若是设置了 filter 为名称为 A.B 的 logger,则该 logger 和其子 logger 的信息会被输出,如 A.B,A.B.C.

清单 12. 日志使用示例

import logging 
 LOG1=logging.getLogger('b.c') 
 LOG2=logging.getLogger('d.e') 
 filehandler = logging.FileHandler('test.log','a') 
 formatter = logging.Formatter('%(name)s %(asctime)s %(levelname)s %(message)s') 
 filehandler.setFormatter(formatter) 
 filter=logging.Filter('b') 
 filehandler.addFilter(filter) 
 LOG1.addHandler(filehandler) 
 LOG2.addHandler(filehandler) 
 LOG1.setLevel(logging.INFO) 
 LOG2.setLevel(logging.DEBUG) 
 LOG1.debug('it is a debug info for log1') 
 LOG1.info('normal infor for log1') 
 LOG1.warning('warning info for log1:b.c') 
 LOG1.error('error info for log1:abcd') 
 LOG1.critical('critical info for log1:not worked') 
 LOG2.debug('debug info for log2') 
 LOG2.info('normal info for log2') 
 LOG2.warning('warning info for log2') 
 LOG2.error('error:b.c') 
 LOG2.critical('critical')

上例设置了 filter b,则 b.c 为 b 的子 logger,所以知足过滤条件该 logger 相关的日志信息会 被输出,而其余不知足条件的 logger(这里是 d.e)会被过滤掉。
清单 13. 输出结果

b.c 2011-11-25 11:07:29,733 INFO normal infor for log1 
 b.c 2011-11-25 11:07:29,733 WARNING warning info for log1:b.c 
 b.c 2011-11-25 11:07:29,733 ERROR error info for log1:abcd 
 b.c 2011-11-25 11:07:29,733 CRITICAL critical info for log1:not worked

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

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 的资料能够参见参考资料。

相关文章
相关标签/搜索