Python pdb调试
1.Python pdb调试的几种方式。
(1) python -m pdb SumTest.py
import pdb def add(a, b): print(a) print(b) c = a + b print(c) add(10, 20)
在命令行执行上述命令,在Linux操作系统中可以使用。
pdb断点就是SumTest.py的第一行。
(2) 在Python交互环境中启用调试
import pdb def add(a, b): print(a) print(b) c = a + b print(c) add(10, 20) pdb.run("add(100,200)")
使用pdb.run方法。
(3) 硬编码方式在代码中添加下面两句话。
import pdb def add(a, b): print(a) print(b) c = a + b print(c) pdb.set_trace() add(10, 20)
pdb断点就是pdb.set_trace()所在的这一行。
2.Python pdb常用调试命令。
(1) h(elp)
打印当前版本Pdb可用的命令。
如果要查询某个命令,可以输入 h [command]。
例如:help l
(2) l(ist)
可以列出当前将要运行的代码。
例如:list 1,10
l(ist) [first [,last]]
List source code for the current file.
Without arguments, list 11 lines around the current line
or continue the previous listing.
With one argument, list 11 lines starting at that line.
With two arguments, list the given range;
if the second argument is less than the first, it is a count.
(3) b(reak)
设置和查看断点。
例如:b(add)
b(7)
b
b(reak) ([file:]lineno | function) [, condition]
With a line number argument, set a break there in the current
file. With a function name, set a break at first executable line
of that function. Without argument, list all breaks. If a second
argument is present, it is a string specifying an expression
which must evaluate to true before the breakpoint is honored.
The line number may be prefixed with a filename and a colon,
to specify a breakpoint in another file (probably one that
hasn't been loaded yet). The file is searched for on sys.path;
the .py suffix may be omitted.
(4) cl(ear)
清除指定断点或者全部断点。
例如:cl 1
cl
cl(ear) filename:lineno
cl(ear) [bpnumber [bpnumber...]]
With a space separated list of breakpoint numbers, clear
those breakpoints. Without argument, clear all breaks (but
first ask confirmation). With a filename:lineno argument,
clear all breaks at that line in that file.
Note that the argument is different from previous versions of
the debugger (in python distributions 1.5.1 and before) where
a linenumber was used instead of either filename:lineno or
breakpoint numbers.
(5) disable/enable
禁用/激活断点。
例如:disable 3
enable 3
disable bpnumber [bpnumber ...]
Disables the breakpoints given as a space separated list of
bp numbers.
enable bpnumber [bpnumber ...]
Enables the breakpoints given as a space separated list of
bp numbers.
(6) a(rgs)
打印当前函数的参数。
例如:a
a(rgs)
Print the arguments of the current function.
(7) p
打印某个变量。
例如:p a
p expression
Print the value of the expression.
(8) w(here)
打印当前堆栈信息。
例如:w
bt
w(here)
Print a stack trace, with the most recent frame at the bottom.
An arrow indicates the "current frame", which determines the
context of most commands. 'bt' is an alias for this command.
(9) q(uit)
退出当前调试。
例如: q
q(uit) or exit - Quit from the debugger.
The program being executed is aborted.
(10) c(ont(inue))
继续执行直到程序结束或者是下一个断点。
例如:c
c(ont(inue))
Continue execution, only stop when a breakpoint is encountered.
(11) s(tep)
执行下一个步骤,如果当前有一个函数调用,那么s会进入被调用的函数体中。
例如:s
s(tep)
Execute the current line, stop at the first possible occasion
(either in a function that is called or in the current function).
(12) n(ext)
执行下一个步骤,如果当前有一个函数调用,那么s不会进入被调用的函数体中。
例如:n
n(ext)
Continue execution until the next line in the current function
is reached or it returns.