Using ipython's debugger as pdb replacement.

Do you know how to debug your python code? I usually do the following in my code to set something like a hardcoded breakpoint:

import pdb; pdb.set_trace();

This will bring up a pdb prompt when the code execution reaches this line. But pdb is a bit limited since it doesn't support code completion, syntax highlighting and so on.

So here is how you make your debug prompt more fancy by using ipython's debugger.

some ipython love for your debugging experience

Like always, make sure first that all dependecies are installed. In this case its ipython 1. Install it with pip install ipython or easy_install ipython.

Now put the following file in somewhere in your PYTHONPATH (for me its ~/.local/lib/python2.6/dist-packages/

import sys
from IPython.Debugger import Pdb
from IPython.Shell import IPShell
from IPython import ipapi

shell = IPShell(argv=[''])

def set_trace():
    ip = ipapi.get()
    def_colors = ip.options.colors

Know you can use in your code the following snippet:

import ipdb; ipdb.set_trace();

This will bring up ipython's debugger instead of the old pdb prompt. Test it out! It's so cool to have code completion while debugging.

1 In case you don't know what ipython is, check their out homepage and try to run ipython in your console after installing it. It's definitly worth exploring. (For those who really don't know what it is: its a fancy, more feature rich python console).


As Andrey mentioned in the comments: there is an easier way now to setup ipdb in your PYTHONPATH. Just install the ipdb package from the PyPI: pip install ipdb


blog comments powered by Disqus

This is Using ipython's debugger as pdb replacement. by Gregor Muellegger and was published on 22nd July 2010.

Short url:

Tags: debugging, django, python

Back to the list page »

Know me …