Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive debugging in IPython (Jupyter) notebook

For debugging my python code, I use the ipdb library, and use the set_trace() command to place a break point. Once the code reaches there, I get an interactive shell with ipdb> prompt, that I can explore local variables with tab autocompletion.

In IPython (Jupyter) notebook, however, ipdb.set_trace() does not work. As suggested by this post: using ipdb to debug python code in one cell (jupyter or Ipython)

I use the following alternative for interactive debugging:

from IPython.core.debugger import Tracer
Tracer()() #this one triggers the debugger

This gives me the ipdb> prompt, but the tab autocomplete is not available. Is there anyway to enable auto-complete for interactive debugging using ipython notebook? This is extremely useful, specially when you have a lot of variables with long names.

like image 311
motam79 Avatar asked Nov 08 '16 15:11

motam79


People also ask

Is debugging possible in Jupyter notebook?

Debug code in Jupyter notebooks The Jupyter Notebook Debugger tool window opens. Debugging is performed within a single code cell. However, if your code cell calls a function from any cell that has been already debugged, you can step into it. The related breakpoints will also work.

Can Jupyter notebooks be interactive?

Jupyter Notebook has support for many kinds of interactive outputs, including the ipywidgets ecosystem as well as many interactive visualization libraries.

How do I debug Python with IPython?

In IPython, perhaps the most convenient interface to debugging is the %debug magic command. If you call it after hitting an exception, it will automatically open an interactive debugging prompt at the point of the exception.


1 Answers

In Python 3.7 you can use breakpoint() function

This function drops you into the debugger at the call site. Specifically, it calls sys.breakpointhook(), passing args and kws straight through. By default, sys.breakpointhook() calls pdb.set_trace() expecting no arguments. In this case, it is purely a convenience function so you don’t have to explicitly import pdb or type as much code to enter the debugger. However, sys.breakpointhook() can be set to some other function and breakpoint() will automatically call that, allowing you to drop into the debugger of choice.

like image 76
Vlad Bezden Avatar answered Nov 05 '22 05:11

Vlad Bezden