Using the Python debugger

A few days ago I found out that using the Python debugger is so easy, I can’t believe I haven’t used it before.

Import the module:

import pdb

Set a breakpoint somewhere in your code:

def some_function(self, x, y, z):
    pdb.set_trace()
    ...

Run your program. Now every time ‘some_function’ is called, the Python interpreter will break. At this point you could:

  • type x to inspect the argument passed to the x parameter
  • hit the ‘n’ button to skip over the next line of code
  • hit the ‘c’ button to resume the program
  • hit the ‘h’ button to get help

Easy enough?

Leave a Reply