Issue
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.
Solution
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.
Answered By - Vlad Bezden
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.