Use IPython magic functions in ipdb shell

不想你离开。 提交于 2020-01-11 15:27:13

问题


When debugging Python script using ipdb my_script.py, I want to use IPython magic functions like %paste, %cd in ipdb debug session shell. Is is possible and how?


回答1:


According to the ipdb Github repo magic IPython functions are not available. Fortunately, the IPython debugger provides a couple of clues of how to get this functionality without launching a separate IPython shell.

Here is what I did to run %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

This way, you can step through your code and have access to all the IPython magic functions via the method find_line_magic(your_magic_function).




回答2:


You can open a IPython shell inside a stack, just like pdb does. Do the following:

  • Import embed() from IPython, and put it inside your code.
  • Run the script

Example:

from IPython import embed

def some_func():
    i = 0
    embed()
    return 0

In Python shell:

>>> te.func()

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

In [1]: %whos

Variable   Type    Data/Info
i          int     0

In [2]:

Was that what you were looking for?



来源:https://stackoverflow.com/questions/16184487/use-ipython-magic-functions-in-ipdb-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!