What is the right way to debug in iPython notebook?

前端 未结 10 1630
不知归路
不知归路 2020-11-30 16:50

As I know, %debug magic can do debug within one cell.

However, I have function calls across multiple cells.

For example,

In[1]:          


        
相关标签:
10条回答
  • 2020-11-30 17:22

    Use ipdb

    Install it via

    pip install ipdb
    

    Usage:

    In[1]: def fun1(a):
       def fun2(a):
           import ipdb; ipdb.set_trace() # debugging starts here
           return do_some_thing_about(b)
       return fun2(a)
    In[2]: fun1(1)
    

    For executing line by line use n and for step into a function use s and to exit from debugging prompt use c.

    For complete list of available commands: https://appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf

    0 讨论(0)
  • 2020-11-30 17:27

    You can always add this in any cell:

    import pdb; pdb.set_trace()
    

    and the debugger will stop on that line. For example:

    In[1]: def fun1(a):
               def fun2(a):
                   import pdb; pdb.set_trace() # debugging starts here
               return fun2(a)
    
    In[2]: fun1(1)
    
    0 讨论(0)
  • 2020-11-30 17:29

    Your return function is in line of def function(main function), you must give one tab to it. And Use

    %%debug 
    

    instead of

    %debug 
    

    to debug the whole cell not only line. Hope, maybe this will help you.

    0 讨论(0)
  • 2020-11-30 17:30

    After you get an error, in the next cell just run %debug and that's it.

    0 讨论(0)
  • 2020-11-30 17:40

    I just discovered PixieDebugger. Even thought I have not yet had the time to test it, it really seems the most similar way to debug the way we're used in ipython with ipdb

    It also has an "evaluate" tab

    0 讨论(0)
  • 2020-11-30 17:41

    You can use ipdb inside jupyter with:

    from IPython.core.debugger import Tracer; Tracer()()
    

    Edit: the functions above are deprecated since IPython 5.1. This is the new approach:

    from IPython.core.debugger import set_trace
    

    Add set_trace() where you need a breakpoint. Type help for ipdb commands when the input field appears.

    0 讨论(0)
提交回复
热议问题