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 pdb小结的更多相关文章

  1. Python 学习小结

    python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...

  2. python pdb调试以及sublime3快捷键设置

    python pdb调试以及sublime3快捷键设置 pdb调试 如果对gdb比较熟悉的话,pdb就很容易上手.以一个demo快速了解常用的调试命令. def test(a): while True ...

  3. python pdb 调试

    命令行 Python -m pdb xxx.py l ----> list 显示当前代码 n ----> next 向下执行一行代码 c ----> continue 继续执行代码 ...

  4. Python字典小结

      字典(dict)结构是Python中常用的数据结构,笔者结合自己的实际使用经验,对字典方面的相关知识做个小结,希望能对读者一些启发~ 创建字典   常见的字典创建方法就是先建立一个空字典,然后逐一 ...

  5. Python工程化小结

    对如何写一个工业级的Python项目作一个top-down小结. 一.项目结构 顶层结构: 文件夹: model可以是项目中的自定义类: utils是一些工程工具,比如log,tracker log存 ...

  6. python pdb 基础调试

    当手边没有IDE,面对着python调试犯愁时,你就可以参考下本文:(pdb 命令调试) 参考:http://docs.python.org/library/pdb.html 和 (pdb)help ...

  7. python pdb模块

    参考文件http://pythonconquerstheuniverse.wordpress.com/category/Python-debugger/ 翻译不是一一对应 Debug功能对于devel ...

  8. Python PDB调试处理

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

  9. 第11.27节 Python正则小结:正则静,静则明,明则虚,虚则无为而无不为也

    正则表达式的章节到此就结束了,老猿现在觉得对我们这些身具程序猿基因特色的人来说,正则表达式应该是蛮可口的开胃小菜. 在写标题时,本来想写"正则表达式小结",后来想了想,百度了一下, ...

随机推荐

  1. Qt之QImageReader

    简述 QImageReader类为从文件或设备读取图像提供了一个独立的接口. 读取图像最常用的方法是通过构造QImage和QPixmap,或通过调用QImage::load()和QPixmap::lo ...

  2. C++ 数字、string 简便互转

    一.数字转为 string 类型 借用 sprintf 函数: char buffer[256]; int counter = 10; sprintf(buffer,"%04i", ...

  3. 使用AFNetworking第三方下载类

    AFNetworking 眼下使用比較多得一个下载库 眼下一直在维护更新,使用的是很easy 不须要加入不论什么关联的库  1.带block形式 内部是任务队列进行下载  就是对operation的一 ...

  4. nj08---process、console

    概念:所有属性都可以在程序的任何地方访问,即全局变量.在JavaScript中,通常window是全局对象,而Node.js的全局对象是global,所有全局变量都是global对象的属性,如:con ...

  5. SharePoint 修改完或制作完一定要发布

    设置了匿名访问但是网站就是需要登录,找了很多问题. 首先想到的映射问题,然后努力检查,最后把代码删掉,然后把站删掉,最后测试出来问题. 点击上方[网站设置] 把修改过的文件发布. 母版也和布局页 一定 ...

  6. P2264 情书(字符串hash90分)

    题目背景 一封好的情书需要撰写人全身心的投入.lin_toto同学看上了可爱的卡速米想对她表白,但却不知道自己写的情书是否能感动她,现在他带着情书请你来帮助他. 题目描述 为了帮助lin_toto,我 ...

  7. CF 965 B. Battleship

    Arkady is playing Battleship. The rules of this game aren't really important.There is a field of n×n ...

  8. Web开发、原生开发、混合开发的区别优势:

    一.Web 应用 Web应用本质上是为移动浏览器设计的基于Web的应用,它们是用普通Web开发语言开发的,可以在各种智能手机浏览器上运行. 优点: 支持设备广泛: 较低的开发成本: 可即时上线: 无内 ...

  9. NodeJS学习笔记 进阶 (4)基于express+muter的文件上传(ok)

    个人总结:这篇文章主要讲了multer插件的使用,类似于formidable,可以用来处理post表单中的文件上传,读完这篇文章需要10分钟. 摘选自网络 概览 图片上传是web开发中经常用到的功能, ...

  10. POJ-2142 The Balance 扩展欧几里德(+绝对值和最小化)

    题目链接:https://cn.vjudge.net/problem/POJ-2142 题意 自己看题吧,懒得解释 思路 第一部分就是扩展欧几里德 接下来是根据 $ x=x_0+kb', y=y_0- ...