Debugging#

I saw this great talk by Nina Zakharenko (handout), I learned most tips on this page from that talk.

pdb#

Python Debugger (pdb) is easy to use, simply put breakpoint() in your .py file and run it (python >= 3.7). Florian Preinstorfer made a great cheatsheet for what to do once you’re inside the debugger.

If you want to enter the regular python REPL from the debug mode type interact.

ipdb#

The vanilla python debugger is good, but it misses some basic features such as tab completion. Luckily there is ipdb which is a (near) drop-in replacement.

To set it up:

  1. In your python environment: install ipdb python -m pip install ipdb
  2. In your shell’s rc file: set python breakpoint to ipdb export PYTHONBREAKPOINT = ipdb.set_trace
  3. In your root folder: Create a .pdbrc with this content (credits to Nina Zahrenko):
# Install IPython: python3 -m pip install ipython

import IPython
from traitlets.config import get_config

cfg = get_config()
cfg.InteractiveShellEmbed.colors = "Linux"  # syntax highlighting
cfg.InteractiveShellEmbed.confirm_exit = False

alias interacti IPython.embed(config=cfg)  # I replaced `interacti` with `ii` 

In Jupyter Notebooks#

In jupyter notebooks you can start the debugger as above, but you can also use the %debug magic. It works like this:

  1. Get an error in a cell
  2. Run %debug in another cell to start the debugger the error point of last run cell