What is the right way to debug in iPython notebook?

PythonPython 2.7Jupyter NotebookIpythonPdb

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

You can use ipdb inside jupyter with:

from IPython.core.debugger import Tracer; Tracer()()

Edit: the functions above are deprecated since IPython 5.1. This is the new approach:

from IPython.core.debugger import set_trace

Add set_trace() where you need a breakpoint. Type help for ipdb commands when the input field appears.

Solution 2 - Python

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

Solution 3 - Python

In Python 3.7 you can use breakpoint() function. Just enter

breakpoint()

wherever you would like runtime to stop and from there you can use the same pdb commands (r, c, n, ...) or evaluate your variables.

Solution 4 - Python

Your return function is in line of def function(main function), you must give one tab to it. And Use

%%debug 

instead of

%debug 

to debug the whole cell not only line. Hope, maybe this will help you.

Solution 5 - Python

You can always add this in any cell:

import pdb; pdb.set_trace()

and the debugger will stop on that line. For example:

In[1]: def fun1(a):
           def fun2(a):
               import pdb; pdb.set_trace() # debugging starts here
           return fun2(a)

In[2]: fun1(1)

Solution 6 - Python

The %pdb magic command is good to use as well. Just say %pdb on and subsequently the pdb debugger will run on all exceptions, no matter how deep in the call stack. Very handy.

If you have a particular line that you want to debug, just raise an exception there (often you already are!) or use the %debug magic command that other folks have been suggesting.

Solution 7 - Python

After you get an error, in the next cell just run %debug and that's it.

Solution 8 - Python

Just type import pdb in jupyter notebook, and then use this cheatsheet to debug. It's very convenient.

c --> continue, s --> step, b 12 --> set break point at line 12 and so on.

> Some useful links: Python Official Document on pdb, Python pdb debugger examples for better understanding how to use the debugger commands.

> Some useful screenshots: enter image description hereenter image description here

Solution 9 - Python

I just discovered PixieDebugger. Even thought I have not yet had the time to test it, it really seems the most similar way to debug the way we're used in ipython with ipdb

enter image description here

It also has an "evaluate" tab

Solution 10 - Python

A native debugger is being made available as an extension to JupyterLab. Released a few weeks ago, this can be installed by getting the relevant extension, as well as xeus-python kernel (which notably comes without the magics well-known to ipykernel users):

jupyter labextension install @jupyterlab/debugger
conda install xeus-python -c conda-forge

This enables a visual debugging experience well-known from other IDEs.

enter image description here

Source: A visual debugger for Jupyter

Solution 11 - Python

In VsCode

File -> Preferances -> Settings -> Open Settings (JSON) [small page icon on the upper right]

Paste this line of code to the end of it

"jupyter.experimental.debugging": true

Now you should see debug option at the top navigation bar

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRexView Question on Stackoverflow
Solution 1 - PythonLeopdView Answer on Stackoverflow
Solution 2 - PythonTevin Joseph K OView Answer on Stackoverflow
Solution 3 - PythonVlad BezdenView Answer on Stackoverflow
Solution 4 - PythonMohit KhuranaView Answer on Stackoverflow
Solution 5 - PythonkrockView Answer on Stackoverflow
Solution 6 - PythonwaterproofView Answer on Stackoverflow
Solution 7 - PythonEscachatorView Answer on Stackoverflow
Solution 8 - PythonfloweraView Answer on Stackoverflow
Solution 9 - PythonAlessandro DentellaView Answer on Stackoverflow
Solution 10 - PythonfugledeView Answer on Stackoverflow
Solution 11 - Pythonmustafa candanView Answer on Stackoverflow