Python/Debugging

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Introduction

There are a lot of options when it comes to debugging python, owing to its introspective and malleable nature.

print / log

If you don't want to interrupt the process, but you want to see what's happening, a few prints or logs will get what you need.

Logging is a good idea because you will, at some point, have to debug code that has already run, and reproducing the exact conditions of the bug may be difficult.

Use the logging module to do all your logging.

pdb

Python comes with the pdb module, which does debugging.

There are several ways to invoke this.

set_trace()

In your code, you can put:

import pdb; pdb.set_trace()

and the debugger will kick off when that line of code is run.

Web Development

With web development, it's tricky to debug. Pylons comes with an interactive debugger that is kicked off when an unhandled exception occurs.

See Also