UP | HOME

Simon Braß

Home-Office Physicist
Atom Icon - atom by Erin Agnoli from the Noun Project

A primer on Python's debugger
PDB is better than print

When we tackle a Python bug, e.g. in a Python script or module, we tend to search the respective source code and decorate it with well-beloved print-statements. Beside that this is a bad approach, which requires access to the source code of the script or module - this does not happen if it is compiled from Cython or byte-compiled - a print-statement does never give any other information than to print its argument's value. Where am I? Why am I at this place? Did something went wrong before or will go wrong afterwards? In principle, if we can point to a code position, we will have already answered most of this questions more or less. For this, we need to manually parse the source code, figure out the program flow and so on, with a lot of assumptions. The answer is so much simpler than print-statements: PDB - the Python debugger.

Before we dive into the wondrous world of pdb, let me give my basic debugging workflows:

Orientation and Inspection
Where am I? Stack trace, source listing. What are the arguments and local variables?
Interruption and Stepping
Interrupt my program at a specific point, with and without an condition. Moving inside my execution in specific ways (with and without conditions).
Manipulation
Change of the exeuction flow? Change of variables? Inserting program code.

We can debug a running program, or inspect its "corpse", post-mortem as the docstrings of pdb call it.

More on its way…

Simon Braß ([email protected])

Created: 05 Jan 2021 and last modified: 2021-01-05 Tue 16:46

Emacs 28.1 (Org mode 9.5.2)

Validate