pythonpython-2.7jupyter-notebookipythonpdb

What is the right way to debug in iPython notebook?


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

However, I have function calls across multiple cells.

For example,

In[1]: def fun1(a)
           def fun2(b)
               # I want to set a breakpoint for the following line #
               return do_some_thing_about(b)

       return fun2(a)

In[2]: import multiprocessing as mp
       pool=mp.Pool(processes=2)
       results=pool.map(fun1, 1.0)
       pool.close()
       pool.join

What I tried:

  1. I tried to set %debug in the first line of cell-1. But it enter into debug mode immediately, even before executing cell-2.

  2. I tried to add %debug in the line right before the code return do_some_thing_about(b). But then the code runs forever, never stops.

What is the right way to set a break point within the ipython notebook?


Solution

  • 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