在ipdb shell中使用IPython魔术函数

Fis*_*tor 21 python debugging ipython

当调试Python脚本使用ipdb my_script.py,我想使用魔法的IPython功能,如%paste,%cdipdb调试会话外壳.是可能的,怎么样?

Pet*_*ren 18

根据ipdb Github repo魔术IPython功能不可用.幸运的是,IPython调试器提供了一些如何在不启动单独的IPython shell的情况下获得此功能的线索.

这是我做的运行%cpaste:

ipdb> from IPython import get_ipython
ipdb> shell = get_ipython()
ipdb> shell.find_line_magic('cpaste')()
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:for i in range(0,5):
:       print i
:--
0
1
2
3
4
Run Code Online (Sandbox Code Playgroud)

这样,您可以单步执行代码并通过该方法访问所有IPython魔术函数find_line_magic(your_magic_function).


Luc*_*iro 14

你可以在堆栈中打开一个IPython shell,就像pdb一样.请执行下列操作:

  • 从IPython导入embed(),并将其放入代码中.
  • 运行脚本

例:

from IPython import embed

def some_func():
    i = 0
    embed()
    return 0
Run Code Online (Sandbox Code Playgroud)

在Python shell中:

>>> te.func()

IPython 1.0.0 -- An enhanced Interactive Python.
(...)

In [1]: %whos

Variable   Type    Data/Info
i          int     0

In [2]:
Run Code Online (Sandbox Code Playgroud)

那是你在寻找什么?

  • 这种方法更容易.您还可以将这两行组合在一起`来自IPython import embed; 嵌入(); import ipdb; 在您的代码中使用ipdb.set_trace()`,现在您可以使用IPython中的所有魔术函数. (8认同)
  • 我想使用魔法函数而不在我的代码中导入任何 IPython 函数。 (2认同)