Neat trick: invoking a Python debugger at an arbitrary point in your program

Do you find yourself occasionally wishing you could freeze a misbehaving program at an arbitrary point in time and then examining what was going on interactively?

That's exactly what the debugger is for, but sometimes it's just too much of a bother to run your program inside it, you have to set breakpoints, etc.

Well there's a really simple alternative: call the debugger from an arbitrary point in your program, like this...

import pdb; pdb.set_trace();

You can then examine the state of your program (e.g., locals, globals).

Note that many complex programs take advantage of Python's interpreted nature and go one step further by actually providing the user a way to hook into an interactive, interpreted Python environment on-demand (e.g., debugging console listening on a given port). This allows you to explore your program, usually a long living framework or daemon, from the inside while it is running.

Twisted does this, for example.

You can get future posts delivered by email or good old-fashioned RSS.
TurnKey also has a presence on Google+, Twitter and Facebook.


Reid Ellis's picture

When I worked on Chandler (at OSAF), we used the set_trace() call of pdb.

Liraz Siri's picture

I just experimented with set_trace() and I have to admit I like it better than"pass", globals(), locals()). Thanks Reid!
Guest's picture

same here, its there a difference with pdb.set_trace() and"pass", globals(), locals())??

Still there is a more interactive option for the terminal with the ipdb library (like the ipython shell that does autocomplete and other nice things)

Michael Foord's picture

I do this (single line - easier to delete afterwards):

    import pdb;pdb.set_trace()

As an added bonus: pip install pdbpp

This replaces (well, adds to the import path ahead of the standard library pdb++ by Antonio Cuni which is a compatible-but-turbocharged version of pdb. It is full of win, awesomeness and rainbow pooping unicorns.

xtian's picture

Michael, I thought you were being facetious about those rainbow-pooping unicorns. Now I have to get the carpet cleaned.

Liraz Siri's picture

Thanks for recommending pdbpp. It rocks. I installed it on my TurnKey-based system as follows::
apt-get install python-setuptools
easy_install pdbpp
Besides the color syntax highlighting, I especially like the new sticky command.
Guest's picture

Heh I have a vom Abbreviation for Set trace. Very Handy?
xtian's picture

I've got a yas snippet for the same thing in Emacs.

(I think I'm going to refer to vim as vom henceforth. Was that deliberate?)

bilbo's picture

When my programs crash and it isn't immediately obvious why, I usually wrap the culprit line in the following:

    while True:
        exec(raw_input('>>> '))

That way I find myself in an interactive enviroment right after the crash. It's not a full debugger, but it works out of the box.

ShawnMilo's picture

You don't even have to touch the script in question.

Assuming your script is named, and you want to break at line 15:

Create a file named .pdbrc (leading period) and enter this into it:


Then run this:

python -m pdb #this will launch the debugger

Even more useful, assuming you only want to break under certain conditions, you can add a statement that evaluates to a boolean:

break, x < 5


Post new comment