My .pdbrc

Sunday 8 April 2007

When I need a Python debugger, I use the standard pdb debugger. I'm very low-tech about it: I set breakpoints by adding this line to my source:

import pdb; pdb.set_trace()

It's the only time I use the Python semicolon, and I always use it for this line.

This breaks me into the debugger when the line is executed, and I can step, examine, and so on, from there. The one aid I have is a small .pdbrc file to define a few handy shortcuts for me:

# Ned's .pdbrc

# Print a dictionary, sorted. %1 is the dict, %2 is the prefix for the names.
alias p_ for k in sorted(%1.keys()): print "%s%-15s= %-80.80s" % ("%2",k,repr(%1[k]))

# Print the member variables of a thing.
alias pi p_ %1.__dict__ %1.

# Print the member variables of self.
alias ps pi self

# Print the locals.
alias pl p_ locals() local:

# Next and list, and step and list.
alias nl n;;l
alias sl s;;l

This defines useful commands for printing members of expressions, members of self, and locals. Not a huge step forward, but helpful nonetheless.

There are other Python debuggers, but whenever I try them, I get tangled up in configurations and connections and IDEs. PDB just works, even though it is very basic. Someday I'll find a better one, but for now it serves my needs.

Update: from the comments, a link to a cool way to get more interesting helper functions into the .pdbrc file: have it read an external Python file.


Kent Johnson 9:13 PM on 8 Apr 2007

I have found winpdb to be easy to set up and use.

Chui 6:23 AM on 9 Apr 2007

Pdb is my favourite too! You're the second person I know in the world who uses pdb.

Tim 9:18 AM on 9 Apr 2007

You should try epdb--it's an enhanced version of pdb with a lot of very nice features:

l and l- for moving backwards and forwards in source, and you can do: func? to get the signature of the object or func?? to get the docstring. It also has tab competion, conditional breakspoints, "until " to continue code execution until a certain point, etc.

Bill Mill 9:36 AM on 9 Apr 2007

@Tim: It might be nice, but it doesn't work on Win32 (and is very hard to find - if you're interested)

Alex 8:52 PM on 9 Apr 2007

If you use pdb with emacs, it tracks the location in the source for you, most of the time, so you don't need the step-and-list aliases.

Paulo Eduardo Neves 11:51 PM on 9 Apr 2007

I agree about the set_trace, but I also can't live withtout this tip for code completion:

Bob Erb 10:57 PM on 12 Apr 2007

I've always felt shamefully simple by inserting "import pdb; pdb.set_trace()" into code I need to debug. Visions of sophisticated breakpoint management, Java IDE users smirking and shaking their heads, teasing whispers that there must be a *better* way of debugging python flash and fade. I won't feel that way tomorrow, though. Simple isn't only better, it's ok, too. Thanks, Ned!

Stephen Emslie 6:04 AM on 17 Apr 2007

>I agree about the set_trace, but I also can't live withtout this tip for code completion:

Thanks Paulo.

For those that want to take this trick a step further, I've submitted a patch to make this behaviour default in pdb. You can find the patch here:

jtr 2:37 PM on 22 Apr 2007

- for k in sorted(%1.keys())
+ for k in sorted(%1)

sorted will call iter for you, and iter(dict) is approximately the same as dict.iterkeys().

Add a comment:

Ignore this:
not displayed and no spam.
Leave this empty:
not searched.
Name and either email or www are required.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.