本文demo文件名为test_pdb.py
启动调试的两种方法
1、在文件中导入pdb模块 并在需要处打断点 pdb.set_trace()
2、运行文件时候命令为python -m pdb test_pdb.py
本文测试文件代码如下
list = [11,22,33,44]
for i in list:
print(i)
str = "hello python"
def func(str):
print("hello world")
print("======%s====" % str)
print("======test end ======")
def main():
func(str)
if __name__ == "__main__":
main()
pdb调试命令
n:执行当前代码,如果本句是定义函数,则跳过,如果是函数调用,不会进入函数
s:执行当前代码,如果本句是定义函数,则跳过,如果是函数调用,则进入函数
b 9:在第9行打断点
b:展示当前所有断点
c:运行代码到下一个断点
disable:停用断点 后面跟断点序号、
enable:激活断点 后面跟断点序号
cl :删除断点 后面跟断点序号
r:执行当前运行函数至结束
l :列出源码
变量名:如果存在当前变量,则打印出当前变量的值
q:退出调试
效果展示:
python@ubuntu:~/Desktop$ python -m pdb test_pdb.py
> /home/python/Desktop/test_pdb.py(1)<module>()
-> list = [11,22,33,44]
(Pdb) n
> /home/python/Desktop/test_pdb.py(3)<module>()
-> for i in list:
(Pdb) s
> /home/python/Desktop/test_pdb.py(4)<module>()
-> print(i)
(Pdb) i
11
(Pdb) b 4
Breakpoint 1 at /home/python/Desktop/test_pdb.py:4
(Pdb) b 8
Breakpoint 2 at /home/python/Desktop/test_pdb.py:8
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at /home/python/Desktop/test_pdb.py:4
2 breakpoint keep yes at /home/python/Desktop/test_pdb.py:8
(Pdb) cl 1
Deleted breakpoint 1
(Pdb) b
Num Type Disp Enb Where
2 breakpoint keep yes at /home/python/Desktop/test_pdb.py:8
(Pdb) c
11
22
33
44
> /home/python/Desktop/test_pdb.py(8)func()
-> print("hello world")
(Pdb) r
hello world
======hello python====
======test end ======
--Return--
> /home/python/Desktop/test_pdb.py(10)func()->None
-> print("======test end ======")
(Pdb) q
python@ubuntu:~/Desktop$