参考文件http://pythonconquerstheuniverse.wordpress.com/category/Python-debugger/

翻译不是一一对应

Debug功能对于developer是非常重要的,python提供了相应的模块pdb让你可以在用文本编辑器写脚本的情况下进行debug. pdb是python debugger的简称。

常用的一些命令如下:

命令 用途
break 或 b 设置断点
continue 或 c 继续执行程序
list 或 l 查看当前行的代码段
step 或 s 进入函数
return 或 r 执行代码直到从当前函数返回
exit 或 q 中止并退出
next 或 n 执行下一行
pp 打印变量的值
help 帮助

开始介绍如何使用pdb。

使用的测试代码1: epdb1.py

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

Enter the debugger at the calling stack frame.
This is useful to hard-code abreakpoint at a given point in a program,
even if the code is not otherwisebeing debugged (e.g. when an assertion
fails).

1 开始调试:

[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)

  1. 使用n+enter表示执行当前的statement,在第一次按下了n+enter之后可以直接按enter表示重复执行上一条debug命令。

If you press ENTER without entering anything, pdb will re-execute the last command that you gave it.

  1. quit或者q可以退出当前的debug,但是quit会以一种非常粗鲁的方式退出程序,直接crash

[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 。。> 错误。

[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的状态。
[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

可以在代码print final之前再加上set_trace()验证。

  • 如果代码过程,在debug的时候不一定能记住当前的代码快,则可以通过使用list或者l命令在显示。list会用箭头->指向当前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:

 epdb2.py --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这句的时候会将其当做普通的赋值语句处理,进入到print final。如果想要对函数进行debug如何处理?可以直接使用s进入函数块。

[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)

如果不想在函数里单步调试可以在断点出直接按r退出到调用的地方。

在调试的时候动态改变值 。注意下面有个错误,原因是b已经被赋值了,如果想重新改变b的赋值,则应该使用!b

[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)

再贴一篇好文章:http://onlamp.com/pub/a/python/2005/09/01/debugger.html?page=1

Debugger Module Contents

The pdb module contains the debugger. pdb containsone class,
Pdb, which inherits from bdb.Bdb. Thedebugger documentation mentions six functions, which create an interactivedebugging session:

pdb.run(statement[, globals[, locals]])
pdb.runeval(expression[, globals[, locals]])
pdb.runcall(function[, argument, ...])
pdb.set_trace()
pdb.post_mortem(traceback)
pdb.pm()

All six functions provide a slightly different mechanism for dropping a userinto the debugger.

pdb.run(statement[, globals[, locals]])

pdb.run() executes the string statement under thedebugger's control. Global and local dictionaries are optional parameters:

#!/usr/bin/env python

import pdb

def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int if __name__ == "__main__":
pdb.run("test_debugger(0)")

pdb.runeval(expression[,globals[, locals]])

pdb.runeval() is identical to pdb.run(), exceptthat pdb.runeval() returns the value of the evaluated stringexpression:

#!/usr/bin/env python

import pdb

def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int if __name__ == "__main__":
pdb.runeval("test_debugger(0)")

pdb.runcall(function[,argument, ...])

pdb.runcall() calls the specified function andpasses any specified arguments to it:

#!/usr/bin/env python

import pdb

def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int if __name__ == "__main__":
pdb.runcall(test_debugger, 0)

pdb.set_trace()

pdb.set_trace() drops the code into the debugger when executionhits it:

#!/usr/bin/env python

import pdb

def test_debugger(some_int):
pdb.set_trace()
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int if __name__ == "__main__":
test_debugger(0)

pdb.post_mortem(traceback)

pdb.post_mortem() performs postmortem debugging of thespecified traceback:

#!/usr/bin/env python

import pdb

def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int if __name__ == "__main__":
try:
test_debugger(0)
except:
import sys
tb = sys.exc_info()[2]
pdb.post_mortem(tb)

pdb.pm()

pdb.pm() performs postmortem debugging of the tracebackcontained in sys.last_traceback:

#!/usr/bin/env python

import pdb
import sys def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int def do_debugger(type, value, tb):
pdb.pm() if __name__ == "__main__":
sys.excepthook = do_debugger
test_debugger(0)

python pdb模块的更多相关文章

  1. [Python]-pdb模块-单步调试

    使用pdb模块辅助python调试. import pdb 断点模式 在需要调试的语句前设置断点,加入这行代码: pdb.set_trace() 程序运行到这就会进入断点调试模式. 输入 作用 n 运 ...

  2. 使用pdb模块调试Python

    在Python中,我们需要debug时,有三种方式: 加log语句.最简单的方式是添加print()语句来输出我们想要获知的状态或者变量,好处是简单容易操作,坏处是debug完了之后,还需要将prin ...

  3. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  4. Python 日志模块实例

    python 打印对象的所有属性值: def prn_obj(obj):     print '\n'.join(['%s:%s' % item for item in obj.__dict__.it ...

  5. Python logging模块无法正常输出日志

    废话少说,先上代码 File:logger.conf [formatters] keys=default [formatter_default] format=%(asctime)s - %(name ...

  6. Python标准模块--threading

    1 模块简介 threading模块在Python1.5.2中首次引入,是低级thread模块的一个增强版.threading模块让线程使用起来更加容易,允许程序同一时间运行多个操作. 不过请注意,P ...

  7. Python的模块引用和查找路径

    模块间相互独立相互引用是任何一种编程语言的基础能力.对于“模块”这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义.对于编译型的语言,比如C#中 ...

  8. Python Logging模块的简单使用

    前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...

  9. Python标准模块--logging

    1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...

随机推荐

  1. WebService_Demo

    简述 使用IDEA开发webservice服务,从零开始一步一步指引你. 服务端开发 首先创建一个webservice项目,如下图 创建完项目后idea会帮我们创建一个类,helloword,我们把它 ...

  2. pc端_移动端_H5_ QQ在线客服链接代码

    PC端:QQ在线客服链接代码 <a href="tencent://message/?uin=1239300678&Site=sc.chinaz.com&Menu=ye ...

  3. uvm_scoreboard——得分

    scoreboard 是验证平台很重要的一部分,因为,验证就是给激励,然后,检查结果.而scoreboard 就是肩负这检查结果的重任.测试用例能不能过,全由scoreboard说了算. A scor ...

  4. linux 删除文件后空间没有释放的解决办法

    清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [root@ticketb ~]# df -hFilesystem          ...

  5. java 核心技术卷一笔记 6 .1.接口 lambda 表达式 内部类

    6.1.2 接口不是类,不能实例化一个接口:但是可以声明接口的变量:Comparable x;    接口变量必须引用实现了接口的类对象:x = new Employee(); 检查一个对象是否属于某 ...

  6. poj1142Smith Numbers质因子分解

    题意:一个数不是质数,其质因子的每位加起来等于该数的每位加起来. /* 题意:一个数的所有质因子的每位相加起来等于该数的每位相加起来且该数不能是质数,那么就是史密斯数 tip:对于分解质因子,只需要判 ...

  7. Gradle环境下导出Swagger为PDF

    更多精彩博文,欢迎访问我的个人博客 说明 我个人是一直使用Swagger作为接口文档的说明的.但是由于在一些情况下,接口文档说明需要以文件的形式交付出去,如果再重新写一份文档难免有些麻烦.于是在网上看 ...

  8. webpack 使用总结

    参考:http://www.ferecord.com/webpack-summary.html#base64 写的比较详细了

  9. shell脚本,编程题练习。

    题目是:将 文件file为 b+b+b+b+b+b+b+b 变为 b+b=b+b=b+b=b+b 解答方法如下:

  10. iMessage, Facetime 解决办法

    不需要白苹果三码,亲测可用:原帖地址: https://www.reddit.com/r/hackintosh/comments/2wohwn/getting_imessage_working_on_ ...