Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Debugging

Code always eventually breaks. Let’s look at some broken code:

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[4], line 1
----> 1 my_broken_function()

Cell In[3], line 7, in my_broken_function()
      3     y = 4
      4     x += 2
      5     y *= 2
      6     x -= y
----> 7     y /= x
      8     return x, y

ZeroDivisionError: division by zero

IPython debugger

Try writing %debug into the cell below! (you can even skip the %)

The mini-language here is pdb, and is similar to gdb and many other debuggers. You can step forward, up, etc. You can set breakpoints, or you can just write breakpoint() anywhere, and the “current” debugger will pick up there!

Rich tracebacks

Another trick comes from the Rich library. You can install a nicer traceback handler. Never do this in a library, but only in applications and user code.

<bound method InteractiveShell.excepthook of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7bae0cda0ec0>>

This needs to be in a file (normally it will be) for the traceback to show up nicely:

Writing tmp_rich.py
Loading...

Debugging in Jupyter Lab

This used to require the Xeus Python kernel instead of IPython, but IPyKernel 6+ now supports the visual debugger protocol directly.

Turn on the debugger with the switch on the top right. Click on the line numbers to set a breakpoint. Then run.

Loading...