Sunday 8 April 2007 — This is over 17 years old. Be careful.
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.
Comments
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.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498182
>http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498182
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: http://sourceforge.net/tracker/index.php?func=detail&aid=1641544&group_id=5470&atid=305470
+ for k in sorted(%1)
sorted will call iter for you, and iter(dict) is approximately the same as dict.iterkeys().
# acknowledgement: stackoverflow.com/questions/173278/
# docs.python.org/3/library/os.html#os._exit
look for "def dispatch_line"
comment out line 69: if self.quitting: raise BdbQuit
add new if clause: if self.quitting: os._exit(1)
Add a comment: