tags:

views:

1178

answers:

13

What are your best tips for debugging Python?

Please don't just list a particular debugger without saying what it can actually do.

Related

+10  A: 

print statements

  • Some people recommend a debug_print function instead of print for easy disabling
  • The pprint module is invaluable for complex structures
hasen j
+1 when every debugger fails, print is your friend, yes debug_print would be nice addition
Anurag Uniyal
I generally print first then debug second except when I *know* I will be able to solve by tracing a particular section
Casebash
Actually the log module does just that.
e-satis
True, but logging has to be set up. I will learn how to use the module after honors
Casebash
+17  A: 

PDB

You can use the pdb module, insert pdb.set_trace() anywhere and it will function as a breakpoint.

>>> import pdb
>>> a="a string"
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None
(Pdb) p a
'a string'
(Pdb)

To continue execution use c (or cont or continue).

It is possible to execute arbitrary Python expressions using pdb. For example, if you find a mistake, you can correct the code, then type a type expression to have the same effect in the running code

ipdb is a version of pdb for IPython. It allows the use of pdb with all the IPython features including tab completion.

It is also possible to set pdb to automatically run on an uncaught exception.

Pydb was written to be an enhanced version of Pdb. Benefits?

ghostdog74
Here is an article on using pdb: http://sontek.net/debugging-python-with-pdb
sontek
+3  A: 

ipdb is like pdb, with the awesomeness of ipython.

Alex Gaynor
Could you add more details on what it can do?
Casebash
tab completion for one.
uswaretech
+6  A: 

It is possible to print what Python lines are executed (thanks Geo!). This has any number of applications, for example, you could modify it to check when particular functions are called or add something like ## make it only track particular lines.

code.interact takes you into a interactive console

import code; code.interact(local=locals())

If you want to be able to easily access your console history look at: "Can I have a history mechanism like in the shell?" (will have to look down for it).

Auto-complete can be enabled for the interpreter.

Casebash
+1  A: 

When possible, I debug using M-x pdb in emacs for source level debugging.

themis
+9  A: 

Logging

Python already has an excellent built-in logging module. You may want to use the logging template here.

The logging module lets you specify a level of importance; during debugging you can log everything, while during normal operation you might only log critical things. You can switch things off and on.

Most people just use basic print statements to debug, and then remove the print statements. It's better to leave them in, but disable them; then, when you have another bug, you can just re-enable everything and look your logs over.

This can be the best possible way to debug programs that need to do things quickly, such as networking programs that need to respond before the other end of the network connection times out and goes away. You might not have much time to single-step a debugger; but you can just let your code run, and log everything, then pore over the logs and figure out what's really happening.

steveha
The problem with logging module is that it heavily breaks with Unicode and various workarounds are needed to have it working within an internationalized applications. Though, this is still the best logging solution for Python.
Jacek Konieczny
+5  A: 

PyDev

PyDev has a pretty good interactive debugger. It has watch expressions, hover-to-evaluate, thread and stack listings and (almost) all the usual amenities you expect from a modern visual debugger. You can even attach to a running process and do remote debugging.

Like other visual debuggers, though, I find it useful mostly for simple problems, or for very complicated problems after I've tried everything else. I still do most of the heavy lifting with logging.

itsadok
Does it have the ability to edit and continue?
Casebash
@CaseBash no it doesn't but that feature is planned. Even without it though, the speed and ease of setting/unsetting breakpoints and looking through variable values is still very useful
Jiaaro
+2  A: 

In Vim, I have these three bindings:

map <F9> Oimport rpdb2; rpdb2.start_embedded_debugger("asdf") #BREAK<esc>
map <F8> Ofrom nose.tools import set_trace; set_trace() #BREAK<esc>
map <F7> Oimport traceback, sys; traceback.print_exception(*sys.exc_info()) #TRACEBACK<esc>

rpdb2 is a Remote Python Debugger, which can be used with WinPDB, a solid graphical debugger. Because I know you'll ask, it can do everything I expect a graphical debugger to do :)

I use pdb from nose.tools so that I can debug unit tests as well as normal code.

Finally, the F7 mapping will print a traceback (similar to the kind you get when an exception bubbles to the top of the stack). I've found it really useful more than a few times.

David Wolever
A: 

print statements are the bad way. Thats why there are debuggers. Use pdb.

Xolve
+3  A: 

Winpdb is very nice, and contrary to its name it's completely cross-platform.

It's got a very nice prompt-based and GUI debugger, and supports remote debugging.

orip
What can it do?
Casebash
@Casebash - added more details
orip
+3  A: 

If you are using pdb, you can define aliases for shortcuts. I use these:

# 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 instance variables of a thing.
alias pi p_ %1.__dict__ %1.

# Print the instance 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

# Short cuts for walking up and down the stack
alias uu u;;u
alias uuu u;;u;;u
alias uuuu u;;u;;u;;u
alias uuuuu u;;u;;u;;u;;u
alias dd d;;d
alias ddd d;;d;;d
alias dddd d;;d;;d;;d
alias ddddd d;;d;;d;;d;;d
Ned Batchelder
How do you define these alias?
Casebash
Wow, that could be incredibly powerful
Casebash
Put this stuff in ~/.pdbrc
Ned Batchelder
on windows you can put it in ~/_ipython/ipythonrc.ini
fastmultiplication
+1  A: 

Getting a stack trace from a running Python application

There are several tricks here. These include

  • Breaking into an interpreter/printing a stack trace by sending a signal
  • Getting a stack trace out of an unprepared Python process
  • Running the interpreter with flags to make it useful for debugging
Casebash
A: 

Defining useful repr() methods for your classes (so you can see what an object is) and using repr() or "%r" % (...) or "...{0!r}..".format(...) in your debug messages/logs is IMHO a key to efficient debugging.

Also, the debuggers mentioned in other answers will make use of the repr() methods.

Jacek Konieczny