使用 IPython 对 Python 进行断点引发的交互式调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14635299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Breakpoint-induced interactive debugging of Python with IPython
提问by Amelio Vazquez-Reina
Say I have an IPython session, from which I call some script:
假设我有一个 IPython 会话,我从中调用了一些脚本:
> run my_script.py
Is there a way to induce a breakpoint in my_script.pyfrom which I can inspect my workspace from IPython?
有没有办法在my_script.py其中引入断点,我可以从 IPython 检查我的工作区?
I remember reading that in previous versions of IPython one could do:
我记得在以前版本的 IPython 中读到过,可以这样做:
from IPython.Debugger import Tracer;
def my_function():
x = 5
Tracer()
print 5;
but the submodule Debuggerdoes not seem to be available anymore.
但子模块Debugger似乎不再可用。
Assuming that I have an IPython session open already: how can I stop my program a location of my choice and inspect my workspace with IPython?
假设我已经打开了一个 IPython 会话:如何在我选择的位置停止我的程序并使用 IPython 检查我的工作区?
In general, I would prefer solutions that do not require me to pre-specify line numbers, since I would like to possibly have more than one such call to Tracer()above and not have to keep track of the line numbers where they are.
一般来说,我更喜欢不需要我预先指定行号的解决方案,因为我希望可能有不止一个这样的调用Tracer(),而不必跟踪它们所在的行号。
采纳答案by pankaj
The Tracer()still exists in ipython in a different module. You can do the following:
在Tracer()仍然存在IPython中在不同的模块。您可以执行以下操作:
from IPython.core.debugger import Tracer
def my_function():
x = 5
Tracer()()
print 5
Note the additional call parentheses around Tracer
注意周围的附加调用括号 Tracer
edit: For IPython 6 onwards Traceris deprecatedso you should use set_trace()instead:
编辑:对于 IPython 6 以后Tracer已弃用,因此您应该set_trace()改用:
from IPython.core.debugger import set_trace
def my_function():
x = 5
set_trace()
print 5
回答by Daniel Roseman
Inside the IPython shell, you can do
在 IPython shell 中,你可以做
from IPython.core.debugger import Pdb
pdb = Pdb()
pdb.runcall(my_function)
for example, or do the normal pdb.set_trace()inside your function.
例如,或pdb.set_trace()在您的函数中执行正常操作。
回答by Wilduck
You can run it and set a breakpoint at a given line with:
您可以运行它并在给定的行上设置一个断点:
run -d -b12 myscript
Where -b12 sets a breakpoint at line 12. When you enter this line, you'll immediately drop into pdb, and you'll need to enter cto execute up to that breakpoint.
其中 -b12 在第 12 行设置了一个断点。当您进入这一行时,您将立即进入 pdb,您需要输入c才能执行到该断点。
回答by dvreed77
I have always had the same question and the best workaround I have found which is pretty hackey is to add a line that will break my code, like so:
我一直有同样的问题,我发现的最好的解决方法是添加一行会破坏我的代码的行,如下所示:
...
a = 1+2
STOP
...
Then when I run that code it will break, and I can do %debug to go there and inspect. You can also turn on %pdb to always go to point where your code breaks but this can be bothersome if you don't want to inspect everywhere and everytime your code breaks. I would love a more elegant solution.
然后当我运行该代码时它会中断,我可以执行 %debug 去那里检查。您还可以打开 %pdb 以始终转到代码中断的点,但是如果您不想在任何地方和每次代码中断时都进行检查,这可能会很麻烦。我想要一个更优雅的解决方案。
回答by Medhat Omr
This is the version using the set_trace()method instead of the deprecated Tracer()one.
这是使用该set_trace()方法的版本,而不是已弃用的版本Tracer()。
from IPython.core.debugger import Pdb
def my_function():
x = 5
Pdb().set_trace()
print 5

