Python调试器(pdb):使用pdb浏览多模块代码

问题描述 投票:0回答:1

我将代码分为三个模块;这些模块如下:1)run_module.py2)module1.py3)module2.py

我有一个if name =='main':run_module.py内的语句,我很舒服地使用CLI来执行python -b pdb run_module.py,然后在run_module内设置断点.py使用(pdb)b行号格式。

我的问题是:如何通过CLI在module1.py或module2.py中设置断点;即。不直接干预module1.py和module2.py脚本并输入import pdb; pdb.set_trace()?

任何见识都将不胜感激。提前非常感谢。

python pdb
1个回答
0
投票

[pdb,例如gdbtrepan3k具有中断命令:

(Pdb) help break
b(reak) ([file:]lineno | function) [, condition]
With a line number argument, set a break there in the current
file.  With a function name, set a break at first executable line
of that function.  Without argument, list all breaks.  If a second
argument is present, it is a string specifying an expression
which must evaluate to true before the breakpoint is honored.

The line number may be prefixed with a filename and a colon,
to specify a breakpoint in another file (probably one that
hasn't been loaded yet).  The file is searched for on sys.path;
the .py suffix may be omitted.

但是执行此操作时,您应该注意一些事项。

如果您通过文件名和行号指定断点,则可能会收到错误消息。例如:

(Pdb) break foo.py
*** The specified object 'foo.py' is not a function
or was not found along sys.path.

让我们尝试理解消息的内容。 foo.py显然不是功能。这是一个文件。是sys.path吗?

(Pdb) import sys
(Pdb) sys.path
['', '/usr/lib/python3.6', ...]

没有好。如果我将文件名指定为absolute路径,该怎么办?

(Pdb) break /tmp/bug.py:2
Breakpoint 1 at /tmp/bug.py:2

好那个有效。但是,还有一个警告:可能在该文件的那一行停下来吗?观看:

(Pdb) break /etc/hosts:1
Breakpoint 2 at /etc/hosts:1

/etc/hosts是文件,但不是Python程序。正如我们之前看到的,pdb警告文件是否丢失,但它不会检查文件是否为Python文件。

[相反,如果您使用trepan3k来运行它,则会打印出一个讨厌的回溯(我会在某个时候解决),但至少它会给人一些暗示错误的提示。

pdb也不知道第2行是否有可在其处停止的代码; pdb将警告有关空行或空行或行注释行,但比这更复杂的内容,例如一些随机文本,没有骰子。

再次trepan3k对此要好一些:

(trepan3k) break /tmp/bug.py:2
** File /tmp/bug.py is not stoppable at line 2.

使用断点时的最后警告是,因为代码永远不会到达断点,所以可能不会命中断点。但是,sys.set_trace()也具有此功能,因此我想这种行为就不足为奇了。

© www.soinside.com 2019 - 2024. All rights reserved.