python - 使用 Python 调试器 (ipdb) 时出现名称错误

标签 python pdb ipdb

我正在尝试更加熟练地使用调试器,并且正在遵循 http://www.onlamp.com/pub/a/python/2005/09/01/debugger.html 中给出的示例。我目前正在尝试这个脚本:

#!/usr/bin/env python

import ipdb

def test_debugger(some_int):
    print "start some int>>", some_int
    return_int = 10 / some_int
    print "end some_int>>", some_int
    return return_int

if __name__ == "__main__":
    ipdb.run("test_debugger(0)")

但是,如果我运行它并尝试按 n,我会收到 NameError:

> <string>(1)<module>()

ipdb> n
NameError: "name 'test_debugger' is not defined"

据我了解 https://docs.python.org/2/library/pdb.html#pdb.run ,应该可以使用n(ext)命令运行,直到出现实际的错误。有人可以解释一下这是怎么回事吗?

最佳答案

从您提到的文档中,解释链接到 https://docs.python.org/2/library/functions.html#eval .

您对 ipdb.run() 的调用似乎没有提供 globalslocals 字典,因此 test_debugger 未在run 的上下文。

你可以让它像这样工作:

ipdb.run("test_debugger(0)", {'test_debugger': test_debugger})

关于python - 使用 Python 调试器 (ipdb) 时出现名称错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225684/

相关文章:

javascript - JavaScript 生态系统中的 .co 文件是什么

python - 是否可以跳过 pdb/ipdb 中的断点?

python - ipdb 显示颜色代码而不是颜色

python - 退出 Python 调试器 ipdb

python - 将字典列表中的两个列表合并为一个

python - 如何使用python从列表Json中获取数据

python - 在 python 项目中不使用 __init__.py 文件是一种不好的做法吗?

python - 使用列表生成器时 Python 3 中的 pdb 模块中可能存在错误

python - 为什么在 python 脚本中更改目录会影响 pdb 中的显示上下文?

python - ipython Notebook调试出现bug时如何更正数值后继续运行?