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.
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.
Jupyter Notebook has support for many kinds of interactive outputs, including the ipywidgets ecosystem as well as many interactive visualization libraries.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With