如何使用 IPython --pdb 调试 Python 脚本?
How to debug Python script with IPython --pdb?
我想使用 pdb
在交互模式下调试为 Sphinx 编写的扩展,这对简单的脚本非常有用。当我执行 sphinx-build -M html . _build
时,我得到一个 ValueError
。所以为了调试它我写道:
ipython3 $(which sphinx-build) --pdb -- -M html . _build
不幸的是我没有得到完整的回溯,只有来自主脚本的回溯 sphinx-build
:
$ ipython3 $(which sphinx-build) --pdb -- -M html . _build
Running Sphinx v3.1.2
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/sphinx/config.py", line 319,
in eval_config_file
execfile_(filename, namespace)
File "/usr/local/lib/python3.6/dist-packages/sphinx/util/pycompat.py", line 88,
in execfile_
exec(code, _globals)
File "/home/canard/test-sphinx/conf.py", line 17, in <module>
raise ValueError()
ValueError
---------------------------------------------------------------------------
SystemExit Traceback (most recent call last)
/usr/local/bin/sphinx-build in <module>()
9 if __name__ == '__main__':
10 sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
---> 11 sys.exit(main())
SystemExit: 2
> /usr/local/bin/sphinx-build(11)<module>()
7 from sphinx.cmd.build import main
8
9 if __name__ == '__main__':
10 sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
---> 11 sys.exit(main())
ipdb>
如何查看完整的追溯?
我试图通过将 raise ValueError()
添加到 conf.py
来简化案例。因此,要重现只需使用 sphinx-quickstart
创建一个新的 sphinx 项目,修改 conf.py
并添加例外。使用此设置,您会遇到同样的问题。调试器在 sys.exit(main())
停止
RTFM : from the Sphinx manual你可能会读到:
Debugging tips : Use the sphinx-build -P option to run pdb on exceptions.
这不会回答您的问题,但会回答您的 X-Y problem...
我想使用 pdb
在交互模式下调试为 Sphinx 编写的扩展,这对简单的脚本非常有用。当我执行 sphinx-build -M html . _build
时,我得到一个 ValueError
。所以为了调试它我写道:
ipython3 $(which sphinx-build) --pdb -- -M html . _build
不幸的是我没有得到完整的回溯,只有来自主脚本的回溯 sphinx-build
:
$ ipython3 $(which sphinx-build) --pdb -- -M html . _build
Running Sphinx v3.1.2
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/sphinx/config.py", line 319,
in eval_config_file
execfile_(filename, namespace)
File "/usr/local/lib/python3.6/dist-packages/sphinx/util/pycompat.py", line 88,
in execfile_
exec(code, _globals)
File "/home/canard/test-sphinx/conf.py", line 17, in <module>
raise ValueError()
ValueError
---------------------------------------------------------------------------
SystemExit Traceback (most recent call last)
/usr/local/bin/sphinx-build in <module>()
9 if __name__ == '__main__':
10 sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
---> 11 sys.exit(main())
SystemExit: 2
> /usr/local/bin/sphinx-build(11)<module>()
7 from sphinx.cmd.build import main
8
9 if __name__ == '__main__':
10 sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
---> 11 sys.exit(main())
ipdb>
如何查看完整的追溯?
我试图通过将 raise ValueError()
添加到 conf.py
来简化案例。因此,要重现只需使用 sphinx-quickstart
创建一个新的 sphinx 项目,修改 conf.py
并添加例外。使用此设置,您会遇到同样的问题。调试器在 sys.exit(main())
RTFM : from the Sphinx manual你可能会读到:
Debugging tips : Use the sphinx-build -P option to run pdb on exceptions.
这不会回答您的问题,但会回答您的 X-Y problem...