Python Debugging Primer

Introduction into using the Python debugger(pdb).


The Python Debugger. The pdb module is used for debugging Python code. The basics are inserting a breakpoint and interacting with the execution of the code.

Inserting a breakpoint

Add this line of code to where you want to insert a breakpoint:

import pdb; pdb.set_trace()

Then when your script executes this line, you are inserting into an interactive debugger in your console.

Navigating PDB

While in the interactive debugger mode, you issue commands to manage the execution of the code and interact with the code.

The most common commands are:

  • n: Go to the next line. This will skip over functions.
  • s: Step into a function instead of continuing over the next line
  • c: Continue execution
  • u: Go up a frame
  • d: Go down a frame
  • w: Print stacktrace
  • unt: Continue execution until a line number is reached greater than the current(useful for getting around loops)
  • l: Show current code block of execution
  • pp: pretty print executed line


Full list of commands:


Define aliases for commands. Put it in a ~/.pdbrc file.

Only works for PDB running ON YOUR MACHINE(sorry vagrant) unless you sync the file over to the pdb you're on.


alias inspect import inspect;print inspect.getsource(%1)

Useful Python Utilities

These are useful when using pdb.

- dir: listing an object's attributes and methods::


- locals: listing the local variables are a particular line of execution::