Use IPython magic functions in ipdb shell

前端 未结 2 968
醉酒成梦
醉酒成梦 2021-01-30 22:53

When debugging Python script using ipdb my_script.py, I want to use IPython magic functions like %paste, %cd in ipdb debug se

2条回答
  •  梦如初夏
    2021-01-30 23:52

    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).

提交回复
热议问题