简体   繁体   English

如何在交互模式下打破异常以调试Python脚本?

[英]How to break in interactive mode on an exception to debug a Python script?

I would like to debug a Python package, let say it is Sphinx. 我想调试一个Python包,可以说它是Sphinx。

when I do sphinx-build everything works utile a point I have an exception. 当我进行sphinx-build所有工作都有效,但有一点例外。 Instead of the Traceback I would like to start an interactive session from there in which I can debug my code. 我想从那里开始一个交互式会话,而不是Traceback,可以在其中调试代码。

Is that possible? 那可能吗?

For instance I am looking for something like: 例如,我正在寻找类似的东西:

$ python-debug sphinx-build arg args 
... 
...
Traceback (oops)
...
>>> print("Oh yeah, now we can play...")

Perhaps pdb might be of use to you. 也许pdb对您有用。 Simply invoke it like so where you want to break into the debugger: 像这样简单地调用它,就可以插入调试器了:

import pdb; pdb.set_trace()

For more details please see the pdb documentation: https://docs.python.org/3/library/pdb.html 有关更多详细信息,请参见pdb文档: https : //docs.python.org/3/library/pdb.html

Edit: Perhaps more relevant to your question: pdb can be invoked on the command-line like so: python3 -m pdb myscript.py 编辑:也许与您的问题更相关:pdb可以像这样在命令行上调用: python3 -m pdb myscript.py

I think this can help you to figure out where to place a debugger: 我认为这可以帮助您确定在哪里放置调试器:

  • Check the line number in the Traceback of the module. 检查模块回溯中的行号。
  • Then open the module in any of the text editor by going into the module location. 然后通过进入模块位置在任何文本编辑器中打开模块。
  • Keep the debugger before that line. 将调试器保留在该行之前。

If your are using python3.7 Then you can simply call this function breakpoint() or you can also do this by using import pdb; pdb.set_trace() 如果您使用的是python3.7,则可以简单地调用此函数breakpoint() ,也可以使用import pdb; pdb.set_trace()来执行此操作import pdb; pdb.set_trace() import pdb; pdb.set_trace() . import pdb; pdb.set_trace()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM