While searching for ways to debug code in a Jupyter notebook, I found a lot of outdated posts. So I decided to quickly write up my findings.
Let’s say we have this piece of code and we want to set a breakpoint between the original answer to the ultimate question of life, the universe and everything stored in
answer and the addition to that answer:
The built-in Python debugger
pdb works just fine in a Jupyter notebook. With
import pdb; pdb.set_trace() we can enter the debugger and get a little interactive prompt.
In the debugger we can then print our variables, evaluate code to inspect the current stack, etc.
> <ipython-input>(4)add_to_life_universe_everything() -> answer += x (Pdb) answer 42 (Pdb) n > <ipython-input>(6)add_to_life_universe_everything() -> return answer (Pdb) answer 54 (Pdb) c
Here, we print
answer, then execute and go to the next line with
answer again and then continue the program with
c. You can get an overview of the commands with
h, or specifically with
ipdb but importing and calling it directly only works in the terminal, not in notebooks (see this issue).
Tracer class was actually the first thing I found when looking for information regarding debugging Jupyter notebooks.
This brings up the debugging shell, but prompts a deprecation warning:
/usr/local/lib/python3.5/site-packages/ipykernel_launcher.py:3: DeprecationWarning: `Tracer` is deprecated since version 5.1, directly use \`IPython.core.debugger.Pdb.set_trace()`.
Turns out we need the
set_trace function from
This works fine and brings us a little bit more comfort (e.g. syntax highlighting) than just using the built-in