TurnKey Linux Virtual Appliance Library

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.

Comments

pdb

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

Liraz Siri's picture

pdb.set_trace() is better

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

pdb

same here, its there a difference with pdb.set_trace() and pdb.run("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)

Another +1 for set_trace

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.py) pdb++ by Antonio Cuni which is a compatible-but-turbocharged version of pdb. It is full of win, awesomeness and rainbow pooping unicorns.

Whoa

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

Liraz Siri's picture

pdbpp rocks!

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.

Heh I have a vom Abbreviation

Heh I have a vom Abbreviation for Set trace. Very Handy?

Nice

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?)

Batteries included

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

try:
    line_that_causes_exception()
except:
    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.

Even better

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

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

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

break awesome.py:15

Then run this:

python -m pdb awesome.py #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 awesome.py:15, x < 5

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <strike> <caption>

More information about formatting options

Leave this field empty. It's part of a security mechanism.
(Dear spammers: moderators are notified of all new posts. Spam is deleted immediately)