本文共 1241 字,大约阅读时间需要 4 分钟。
python pdb 使用
使用命令 python -m pdb your_py_file 进入调试界面,输入 help pdb可以查询完整帮助信息
执行命令行debugpython -m pdb your_python_script
常用命令
命令功能break 或 b n设置断点
b显示断点编号
disable n失能第n个断点
cl n删除第n个断点
continue 或 c继续执行程序, 或是跳到下个断点
list 或 l file:n查看当前行的代码段, 指定文件的指定行
jump 或 j跳过指定行,中间代码不执行,跳转后继续运行直到断点处
step 或 s进入函数
return 或 r执行代码直到从当前函数返回
exit 或 q中止并退出
next 或 n执行下一行
p 或!打印变量的值,例如p a
help 或 h帮助
条件断点
1.在设置断点时指定条件b file:line_number, your_condition
# for example
b 10, i==8
2.先设置普通断点, 再设置条件b line_number
condition your_condition
条件断点的组合与条件
condition i==8 and j==8
Note 条件可以设置为condition i=2, 注意中间没有if, condition if i==2是错误的,但是设置通过,没有错误没有警告,只是无法正常执行条件断点
使用调试脚本
在当前目录下创建.pdbrc文件,文件里面是调试脚本。加载pdb时会首先执行里面的命令。
原始的帮助信息如下If a file ".pdbrc" exists in your home directory or in the current
directory, it is read in and executed as if it had been typed at the
debugger prompt. This is particularly useful for aliases. If both
files exist, the one in the home directory is read first and aliases
defined there can be overridden by the local file.
似乎是为了支持别名而不是为了使能调试脚本,不管了,可以使用调试脚本就可以
以下是我测试用的调试脚本b 10 , i==8 and j==8
c
!print('i is ', i)
!print('j is ', j)
q
脚本中出现!前缀的,表明是python代码,用来和普通的调试指令区分开
另外,.pdbrc文件并不是说文件名后缀是.pdbrc,而是整个文件名。
windows系统下无法创建该文件的话,请使用git bash脚本工具
Reference:
转载地址:https://blog.csdn.net/weixin_39927214/article/details/110243710 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!