【问题标题】:Python Debugger (pdb): Navigating through multi-module code using pdbPython 调试器 (pdb):使用 pdb 浏览多模块代码
【发布时间】:2020-04-18 21:34:15
【问题描述】:

我有一个分成三个模块的代码;模块如下: 1) run_module.py 2)模块1.py 3) 模块2.py

我在 run_module.py 中有一个 if name == 'ma​​in': 语句,我很乐意使用我的 CLI 来执行 python -b pdb run_module.py然后使用 (pdb) b 行号格式在 run_module.py 中设置断点。

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

任何见解将不胜感激。非常感谢。

【问题讨论】:

  • 我看到了那个帖子,不,它没有帮助;我试了一下,我得到了以下信息: ***指定的对象 [文件名] 不是函数或未在 sys.path 中找到。 *** 我发现一个帖子也解决了这个案例,这对我也不起作用。我觉得我错过了明显的东西。想法?您还需要我提供什么来给我答复吗?
  • 您可以将文件结构添加到您的问题中吗?

标签: python pdb


【解决方案1】:

pdb,如gdb,或trepan3k 有中断命令:

(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', ...]

没有。好的。那么如果我将文件名作为绝对路径呢?

(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() 也有这种情况,所以我想这种行为并不令人意外。

trepan3kpdb 的断点之间还有一个值得注意的区别。在pdb 中,当您在函数上设置断点时,您实际上是在为该函数记录的第一行设置断点。也就是说,你已经进入了函数。

在 trepan3k 中,您将其设置在函数 call 上,该函数与它所在的位置无关。此外,在对象的上下文中给出的方法名称是一种方法也是可能的,我经常使用它。这是一个例子:

trepan3k /tmp/pathtest.py
(/tmp/pathtest.py:1): <module>
-> 1 from pathlib import Path
(trepan3k) n
(/tmp/pathtest.py:2 @12): <module>
-- 2 mypath = Path(__file__)
(trepan3k) n
(/tmp/pathtest.py:3 @20): <module>
-- 3 print(mypath.match("p.*"))
(trepan3k) break mypath.match()
Breakpoint 1 set at line 972 of file /home/test/.pyenv/versions/3.8.10/lib/python3.8/pathlib.py
(trepan3k) c
(/home/test/.pyenv/versions/3.8.10/lib/python3.8/pathlib.py:972): match
xx 972     def match(self, path_pattern):
(trepan3k) path_pattern
'p.*'
(trepan3k) info pc
PC offset is -1.

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    相关资源
    最近更新 更多