【Python】debug工具-pdb(转)
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)
- 使用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.
- 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】debug工具-pdb(转)的更多相关文章
- Python Debug工具
最近在github上冒出了一个python的debug神器PySnooper,号称在debug时可以消灭print.那么该工具有哪些优点呢,如何使用该工具呢.本文就介绍该工具的优缺点和使用方式. 前言 ...
- 极简Python DeBug工具——PySnooper
DeBug Python 代码的方式有很多种?比如: (1)设置断点 (2)print函数 (3)... 本文要介绍的是一个新开源的项目PySnooper ,只要给有疑问的代码加上装饰器,各种信息一目 ...
- Python 代码使用pdb调试技巧
Debug 对于任何开发人员都是一项非常重要的技能,它能够帮助我们准确的定位错误,发现程序中的 bug.python 提供了一系列 debug 的工具和包,可供我们选择.本文将主要阐述如何利用 pyt ...
- 【Machine Learning】Python开发工具:Anaconda+Sublime
Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...
- Python 开发工具推荐
对于开发工具,仁者见仁智者见智,关键是自己喜欢,用着顺手就好,不用刻意去追求别人用的是什么工具. 这里给大家主要推荐三款工具,分别是PyCharm.Sublime Text 3.VS Code,因为这 ...
- Python开发工具PyCharm个性化设置(图解)
Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧. JetBrains PyCharm Pro 4.5.3 中文 ...
- Yii2的Debug工具
yii2的Debug工具 调用 r=debug 可以对程序进行性能分析,从而对程序进行改良 (1)数据库某条sql语句的执行时间 (2)debug的profiling标签 \YII::beginP ...
- C#报错:创建调试信息文件 ……obj\Debug\model.pdb: 拒绝访问
错误:创建调试信息文件“.......\obj\Debug\model.pdb”时发生错误 --“......\obj\Debug\model.pdb: 拒绝访问. 解决办法如下: 删除该项目下的 b ...
- yii2 debug工具条不出现
UrlManger美化后,debug工具条不出现,禁用UrlManager出现
随机推荐
- JavaScript数组去重方法及测试结果
最近看到一些人的去面试web前端,都说碰到过问JavaScript数组去重的问题,我也学习了一下做下总结. 实际上最有代表性也就三种方法:数组双重循环,对象哈希,排序后去重. 这三种方法我都做了性能测 ...
- 安装ecshop的问题处理
在安装Ecshop的时候,会遇到几个问题: 1.Strict Standards: Non-static method cls_image::gd_version() should not be ca ...
- Ubuntu常用软件包管理命令
1.查看软件包xxx安装内容 #dpkg -L xxx 例子: #dpkg -L binutils //查看安装binutils包会安装哪些文件 2.列出系统已安装的或指定deb包的安 ...
- Java IO学习笔记五
管道流 管道流的主要作用是可以进行两个线程间的通讯,分为管道输出流(PipedOutputStream).管道输入流(PipedInputStream),如果想要进行管道输出,则必须要把输出流连在输入 ...
- Cordova各个插件使用介绍系列(二)—$cordovaBarcodeScanner扫描二维码与生成二维码
详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-2-cordovabarcodescanner/ 这是 ...
- react-native —— 在Mac上配置React Native Android开发环境排坑总结
配置React Native Android开发环境总结 1.卸载Android Studio,在终端(terminal)执行以下命令: rm -Rf /Applications/Android\ S ...
- java 二叉树实现
//二叉树,有左子数,右子树,小的值放到左边,大的值放到右边,左边有树的话继续这个过程,(以下为代部分,赋值的时候采用了递归的方式) package liaobiao;class BiTree{ pr ...
- QPS的计算方法
每秒查询率QPS是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准,在因特网上,作为域名系统服务器的机器的性能经常用每秒查询率来衡量. 原理:每天80%的访问集中在20%的时间里,这20%时 ...
- 2.Smarty的引入和实例化
1.把demo和lib复制出来,并且创建一个test文件夹作为工作的目录 如图所示: 2.这是libs里面的内容,其中smarty.class.php包含了smarty各种方法和功能,需要实例化它还工 ...
- c# ProxyServer 代理服务器 不是很稳定
/**C# Programming Tips & Techniquesby Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill ...