Saturday, August 11, 2007

Python: Coding in the Debugger for Beginners

Python has a wonderful interactive interpreter (i.e. shell). IPython is a third-party Python shell that's even nicer, but that's a topic for another post. It's fairly common to code in the shell until you have the code working correctly, and then copy-and-paste it into your program. Developing super-interactively is a great way to keep bugs at bay.

However, sometimes you need more setup before you can start coding. For instance, when writing a Web app in, say Pylons, you might need an actual request and a database connection before you can start coding what you want. You might even need a form POST before you can start. Ideally, you'd be able to start the shell from in the middle of your application at just the right spot. I'm pretty sure that someone out there knows how to get IPython to do the right thing, but I find using pdb, the Python debugger, really helpful for this purpose.

First of all, add the following wherever you want to break into the debugger, "import pdb; pdb.set_trace()". If you need some more help about the debugger itself, type "?". Now, you'll want to see what variables you have to play with, so use "dir()". Let's say the "request.environ" object has what you want, but you're not sure under what key. Well, start by printing it, "p request.environ". If you need a nicer looking version, try "from pprint import pprint; pprint(request.environ)". If you find the right key, say 'spam', then print that, "pprint(request.environ['spam'])". Maybe spam is an object, and you need to know what methods it has, "pprint(dir(request.environ['spam']))".

In the Python shell, you can use "help(x)" to find out more about x if x is a module, class, or function. If x is an object, try "help(x.__class__)". In the debugger, help() tends to not work for a reason that someone smarter than me probably understands. However, you can still do things like "p x.__class__.__doc__".

The debugger isn't very good for writing new functions or multi-line statements, but it's great for evaluating simple expressions. Once you have the code you want, you can copy-and-paste it back into your program and repeat the process. Writing code in this way is really helpful so that you can deal with the details one at a time without trying to debug a larger program that might have more than one bug. That's not to say I don't make heavy use of print-statement debugging, but using pdb is great if you want to "look around."

6 comments:

Anonymous said...

The pretty printer can be invoked in the debugger directly, without the import, by the 'pp' command

Paddy3118 said...

Anonymous must have pp autoloaded as part of his setup.
The default does not have the prettyprinter loaded.
- Paddy.

Ian said...

pp works for me too, I don't remember having to configure anything for that. Just looked at my .pdbrc -- lots of other stuff but not pp.

pp is in the standard python.org list of debugger commands.

Shannon -jj Behrens said...

> The pretty printer can be invoked in the debugger directly, without the import, by the 'pp' command

Oh, helpful. Thanks!

Jordan G said...

Since you mentioned IPython, I thought I'd drop this in here:

Its really easy to embed IPython into your programs and use them in a similar (but MUCH more powerful way) to the debugger.
Just put:
from IPython.Shell import IPShellEmbed
at the top of your program, and when you would break into the debugger, run:
shell=IPShellEmbed()
shell()

This allows you to poke at the state of your program with all the power of IPython!

Shannon -jj Behrens said...

> This allows you to poke at the state of your program with all the power of IPython!

Awesome! That's the magical incantation I was looking for!