Back to Python#Debugging_and_Profiling

See Also Python Standard Library

Python Library - pdb

1. Sample Code

启动调试 python -m pdb myscript.py

1.1. Debugger Commands

Debugger Commands

Command

Info

h(elp)

无参数显示所有命令

w(here)

程序运行的当前代码位置

d(own)

u(p)

b(reak) function[, condition

用来设置断电

tbreak function[, condition

设置临时断点

cl(ear)

清除所有断点

s(tep)

Step in - 进入相应函数内

n(ext)

单步执行

unt(il)

r(eturn)

Step out?

c(ont(inue))

继续执行,直到结束或遇到断点

j(ump) lineno

跳到某行

l(ist) [first[, last]]

显示断点周围的代码

a(rgs)

显示当前函数的变量值

p expression)

显示对应变量的值

pp expression

美化显示

run [args …]

重新debug

q(uit)

退出

def func():
    print('enter func()')
 
a = 1
b = 2
import pdb
pdb.set_trace()
func()
c = 3
print(a + b + c)
 
# pdb
> /Users/jingxiao/test.py(9)<module>()
-> func()
(pdb) s
--Call--
> /Users/jingxiao/test.py(1)func()
-> def func():
(Pdb) l
  1  -> def func():
  2             print('enter func()')
  3
  4
  5     a = 1
  6     b = 2
  7     import pdb
  8     pdb.set_trace()
  9     func()
 10     c = 3
 11     print(a + b + c)
 
(Pdb) n
> /Users/jingxiao/test.py(2)func()
-> print('enter func()')
(Pdb) n
enter func()
--Return--
> /Users/jingxiao/test.py(2)func()->None
-> print('enter func()')
 
(Pdb) n
> /Users/jingxiao/test.py(10)<module>()
-> c = 3

2. Reference

MainWiki: python_lib_pdb (last edited 2009-02-02 04:23:42 by twotwo)