TIL: Boosting Your Python Debugging with `ipdb`
ipdb supercharges Python’s built-in pdb debugger by integrating it with
IPython. It offers all pdb’s core features, plus:
- Syntax Highlighting: Enhanced readability.
- Tab Completion: Faster command and variable input.
- Better History: No retyping commands.
- Automatic Pretty-Printing: Clearer display of complex data.
- IPython Magic Commands: Direct access to powerful IPython features.
Essentially, ipdb makes debugging more intuitive, efficient, and visually
appealing, moving beyond pdb’s limitations. It’s a fantastic upgrade for any
Python project!
ipdb can be installed like any other python package
pip install ipdb
# or with `uv`:
uv add ipdbTo use this in python, launch python with the env var
PYTHONBREAKPOINT=ipdb.set_trace, so that when the command breakpoint() is
encountered, it’ll launch ipdb instead of pdb
Using ipython, I’ve inserted this block into a startup script in
~/.ipython/profile_default/startup/start.py:
try:
import ipdb
import os
os.environ["PYTHONBREAKPOINT"] = "ipdb.set_trace"
except ImportError:
passSo that when I’m using ipython in a project with ipdb, ipdb is
automatically loaded!
To use the improved debugger in pytest --pdb, add this line to the
[tool.pytest.ini_options] section of the pyproject.toml:
addopts = "--pdbcls=IPython.terminal.debugger:TerminalPdb"