I've recently discovered the very useful '-i' flag to Python
我最近发现了Python非常有用的'-i'标志
-i : inspect interactively after running script, (also PYTHONINSPECT=x) and force prompts, even if stdin does not appear to be a terminal
this is great for inspecting objects in the global scope, but what happens if the exception was raised in a function call, and I'd like to inspect the local variables of the function? Naturally, I'm interested in the scope of where the exception was first raised, is there any way to get to it?
这对于检查全局范围内的对象非常有用,但是如果在函数调用中引发异常会发生什么,并且我想检查函数的局部变量?当然,我对首次提出异常的范围感兴趣,有什么办法可以实现吗?
3 个解决方案
#1
At the interactive prompt, immediately type
在交互式提示符下,立即键入
>>> import pdb
>>> pdb.pm()
pdb.pm() is the "post-mortem" debugger. It will put you at the scope where the exception was raised, and then you can use the usual pdb commands.
pdb.pm()是“post-mortem”调试器。它会将您置于引发异常的范围,然后您可以使用通常的pdb命令。
I use this all the time. It's part of the standard library (no ipython necessary) and doesn't require editing debugging commands into your source code.
我经常用这个。它是标准库的一部分(不需要ipython),也不需要在源代码中编辑调试命令。
The only trick is to remember to do it right away; if you type any other commands first, you'll lose the scope where the exception occurred.
唯一的诀窍就是记住马上做;如果先键入任何其他命令,则会丢失发生异常的范围。
#2
In ipython, you can inspect variables at the location where your code crashed without having to modify it:
在ipython中,您可以在代码崩溃的位置检查变量,而无需修改它:
>>> %pdb on
>>> %run my_script.py
#3
use ipython: http://mail.scipy.org/pipermail/ipython-user/2007-January/003985.html
使用ipython:http://mail.scipy.org/pipermail/ipython-user/2007-January/003985.html
Usage example:
from IPython.Debugger import Tracer; debug_here = Tracer()
#... later in your code
debug_here() # -> will open up the debugger at that point.
"Once the debugger activates, you can use all of its regular commands to step through code, set breakpoints, etc. See the pdb documentation from the Python standard library for usage details."
“一旦调试器激活,您可以使用其所有常规命令来逐步执行代码,设置断点等。有关使用详细信息,请参阅Python标准库中的pdb文档。”
#1
At the interactive prompt, immediately type
在交互式提示符下,立即键入
>>> import pdb
>>> pdb.pm()
pdb.pm() is the "post-mortem" debugger. It will put you at the scope where the exception was raised, and then you can use the usual pdb commands.
pdb.pm()是“post-mortem”调试器。它会将您置于引发异常的范围,然后您可以使用通常的pdb命令。
I use this all the time. It's part of the standard library (no ipython necessary) and doesn't require editing debugging commands into your source code.
我经常用这个。它是标准库的一部分(不需要ipython),也不需要在源代码中编辑调试命令。
The only trick is to remember to do it right away; if you type any other commands first, you'll lose the scope where the exception occurred.
唯一的诀窍就是记住马上做;如果先键入任何其他命令,则会丢失发生异常的范围。
#2
In ipython, you can inspect variables at the location where your code crashed without having to modify it:
在ipython中,您可以在代码崩溃的位置检查变量,而无需修改它:
>>> %pdb on
>>> %run my_script.py
#3
use ipython: http://mail.scipy.org/pipermail/ipython-user/2007-January/003985.html
使用ipython:http://mail.scipy.org/pipermail/ipython-user/2007-January/003985.html
Usage example:
from IPython.Debugger import Tracer; debug_here = Tracer()
#... later in your code
debug_here() # -> will open up the debugger at that point.
"Once the debugger activates, you can use all of its regular commands to step through code, set breakpoints, etc. See the pdb documentation from the Python standard library for usage details."
“一旦调试器激活,您可以使用其所有常规命令来逐步执行代码,设置断点等。有关使用详细信息,请参阅Python标准库中的pdb文档。”