Python debugging

Oddly enough, both better and worse than Perl debugging – better architected in my opnion, but poorly explained and evangelized – to the point where Python book and article writers say “use print statements” to debug code, instead of the debugger.

Quick overview

From the command-line:

python -m pdb yourcode.py

This will inject the pdb module into your program, and will start debugging at the first line of your program as if a pdb.set_trace() call had been issued before the first line of your code.

From inside your code:

import pdb
...
pdb.set_trace()

Importing the pdb module gives you access to the debugger features. set_trace() will set an unconditional breakpoint, dropping you into the interactive debugger prompt. Obviously, this requires a console to exist.

Reading list

Simple introduction: Debugging in Python.

From the Python Documentation: 26.2. pdb – The Python Debugger.

Another article: Interactive Debugging in Python

A more in-depth article by Doug Hellmann: pdb – Interactive Debugger.

Adding features to pdb: Python pdb (debugger) disp equivalent?

Python Debugger Cheatsheet – one-page PDF

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>