Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using ipdb to debug python code in one cell (jupyter or Ipython)

I'm using jupyter (or Ipython) notebook with firefox, and want to debug some python code in the cell. I am using 'import ipdb; ipdb.set_trace()' as kind of breakpoint, for example my cell has the following code:

a=4 import ipdb; ipdb.set_trace() b=5 print a print b 

which after execution with Shift+Enter gives me this error:

-------------------------------------------------------------------------- MultipleInstanceError                     Traceback (most recent call last) <ipython-input-1-f2b356251c56> in <module>()       1 a=4 ----> 2 import ipdb; ipdb.set_trace()       3 b=5       4 print a       5 print b  /home/nnn/anaconda/lib/python2.7/site-packages/ipdb/__init__.py in <module>()      14 # You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.      15  ---> 16 from ipdb.__main__ import set_trace, post_mortem, pm, run, runcall, runeval, launch_ipdb_on_exception      17       18 pm                       # please pyflakes  /home/nnn/anaconda/lib/python2.7/site-packages/ipdb/__main__.py in <module>()      71         # the instance method will create a new one without loading the config.      72         # i.e: if we are in an embed instance we do not want to load the config. ---> 73         ipapp = TerminalIPythonApp.instance()      74         shell = get_ipython()      75         def_colors = shell.colors  /home/nnn/anaconda/lib/python2.7/site-packages/traitlets/config/configurable.pyc in instance(cls, *args, **kwargs)     413             raise MultipleInstanceError(     414                 'Multiple incompatible subclass instances of ' --> 415                 '%s are being created.' % cls.__name__     416             )     417   MultipleInstanceError: Multiple incompatible subclass instances of TerminalIPythonApp are being created. 

The same error appears if I use this code not in the jupyter notebook in the browser, but in jupyter qtconsole. What does this error mean and what to do to avoid it? Is it possible to debug code in the cell step-by-step, using next, continue, etc commands of pdb debugger?

like image 755
lugger1 Avatar asked Feb 24 '16 21:02

lugger1


People also ask

How do you debug Python code in Jupyter notebook?

Debug code in Jupyter notebooksSet the breakpoints in the selected cell and press Alt + Shift + Enter for Windows or ⌥⇧↩ for macOS. Alternatively, you can right-click the cell and select Debug Cell from the context menu. The Jupyter Notebook Debugger tool window opens.

How do I debug a Python file in Jupyter lab?

JupyterLab also has the terminal available so you could just use that and your standard debugging routes too. For example, you can use the terminal to launch IPython and then run some . py code with %run and then when an error is encountered, type %debug to do a post-mortem analysis.


2 Answers

Had this problem also and it seems to be related to versions of jupyter and ipdb.

Solution is to use this instead of the ipdb library set_trace call:

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

Source: http://devmartin.com/blog/2014/10/trigger-ipdb-within-ipython-notebook/

Annotated screenshot: screenshot illustrate how Tracer()() will cause Jupyter notebook to react. It pauses execution on the line in your code where you Trace()() and a new "inline" input accepts ipdb commands like 'p' or 'n' or 'c', shown here

like image 167
Robert Muil Avatar answered Oct 05 '22 22:10

Robert Muil


Tracer() is deprecated.

Use:

from IPython.core.debugger import set_trace

and then place set_trace() where breakpoint is needed.

from IPython.core.debugger import set_trace  def add_to_life_universe_everything(x):     answer = 42     set_trace()     answer += x      return answer  add_to_life_universe_everything(12) 

This works fine and brings us a little bit more comfort (e.g. syntax highlighting) than just using the built-in pdb.

source

like image 24
Mattijn Avatar answered Oct 05 '22 22:10

Mattijn