python pdb调试多进程_PythonPython调试器:使用_pdb_进行逐步调试

示例

Python标准库包含一个称为pdb的交互式调试库。pdb具有广泛的功能,其中最常用的是“逐步执行”程序的功能。

要立即进入逐步调试,请使用:

python -m pdb 

这将在程序的第一行启动调试器。

通常,您将需要针对代码的特定部分进行调试。为此,我们导入pdb库,并使用它set_trace()来中断此问题示例代码的流程。

import pdb

def divide(a, b):

pdb.set_trace()

return a/b

#这怎么了 提示:2!= 3

print divide(1, 2)

运行该程序将启动交互式调试器。

pythonfoo.py

> ~/scratch/foo.py(5)divide()

-> return a/b

(Pdb)

通常此命令用在一行上,因此可以用单个#字符将其注释掉

import pdf; pdb.set_trace()

在(Pdb)提示符下,可以输入命令。这些命令可以是调试器命令或python。要打印变量,我们可以使用调试器中的p或python的print。

(Pdb) p a

1

(Pdb) print a

1

要查看所有局部变量的列表,请使用

locals

内置功能

这些是要了解的好调试器命令:

b  | : set breakpoint at line *n* or function named *f*.

# b 3

# b分

b: show all breakpoints.

c: continue until the next breakpoint.

s: step through this line (will enter a function).

n: step over this line (jumps over a function).

r: continue until the current function returns.

l: list a window of code around this line.

: print variable named *var*.

# 像素

q: quit debugger.

bt: print the traceback of the current execution call stack

up: move your scope up the function call stack to the caller of the current function

down: Move your scope back down the function call stack one level

step: Run the program until the next line of execution in the program, then return control back to the debugger

next: run the program until the next line of execution in the current function, then return control back to the debugger

return: run the program until the current function returns, then return control back to the debugger

continue: continue running the program until the next breakpoint (or set_trace si called again)

调试器还可以交互式评估python:

-> return a/b

(Pdb) p a+b

3

(Pdb) [ str(m) for m in [a,b]]

['1', '2']

(Pdb) [ d for d in xrange(5)]

[0, 1, 2, 3, 4]

注意:

如果您的任何变量名与调试器命令一致,请使用感叹号' !'在var之前以显式引用变量而不是debugger命令。例如,经常会发生这种情况,您将变量名' c '用于计数器,并且您可能想在调试器中打印它。一个简单的“ c ”命令将继续执行直到下一个断点。而是使用' !c '打印变量的值,如下所示:

(Pdb) !c

4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值