python debug怎么用_基本python debug使用(单步调试)

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:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值