这是一个旧的项目,我没有写它,但是它做了一些类似于你想要使用ipython的东西。在'''Start an IPython shell (for debugging) with current environment.
Runs Call db() to start a shell, e.g.
def foo(bar):
for x in bar:
if baz(x):
import ipydb; ipydb.db() #
.
'''
import inspect,IPython
def db():
'''Start IPython shell with callers environment.'''
# find callers
__up_frame = inspect.currentframe().f_back
eval('IPython.Shell.IPShellEmbed([])()', # Empty list arg is
# ipythons argv later args to dict take precedence, so
# f_globals() shadows globals(). Need globals() for IPython
# module.
dict(globals().items() + __up_frame.f_globals.items()),
__up_frame.f_locals)
Jim Robert编辑(问题所有者):为了这个例子,如果您将上面的代码放入一个名为my_debug.py的文件中。然后将该文件放在python路径中,您可以在代码中的任何位置插入以下行以跳转到调试器(只要从shell执行):
^{pr2}$