后浪云Python教程:python中pdb的启用

后浪云Python教程:python中pdb的启用插图

1、首先准备程序,然后启动

python -m pdb err.py
> /Users/michael/Github/learn-python3/samples/debug/err.py(2)<module>()
-> s = '0'

2、输入命令n可以单步执行代码。

3、可以随时输入命令p变量名来查看变量。

4、输入命令q,完成调试退出程序。

我们只需要import pdb,然后在可能出错的地方放一个pdb.set_trace(),就可以设置一个断点:

# err.py
import pdb
 
s = '0'
n = int(s)
pdb.set_trace() # 运行到这里会自动暂停
print(10 / n)

以上就是python中pdb的启用,希望对大家有所帮助。更多Python学习指路:后浪云python教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

THE END