Enhancing Python Breakpoint Debugging

with iPython and ipdb

pdb is Python's brilliant interactive source code debugger which allows you set a point to "jump into" your code and use the standard shell for introspection and navigating up and down your code. IPython is a 3rd-party package (part of SciPy) that adds serious enhancements to Python's interactive shell.

A great tip I learnt about a year ago is that the two can be brought together to make debugging even easier than it already was. ipdb brings all of IPythons greatness including tab completion, syntax highlighting, better tracebacks, better introspection and more to the standard pdb debugger so that when you set your break point, instead of getting the standard Python shell, you receive the IPython one and then effectively carry on as usual. and it's a cinch to install.

Installation and use of ipdb

Prerequisites: the ipython package is required; all methods listed below will install this for you if the requirement is not found.

ipdb is available in the cheeseshop and as such is a standard Python package that can be downloaded and installed with python setup.py install. Or, take the quicker route and install with either easy_install or pip (recommended) with one of the following commands:

    easy_install ipdb
    pip install ipdb

Then in your code, instead of importing pdb you simply import and use ipdb instead as follows:

    import ipdb; ipdb.set_trace()

This comes in particular handy when using Django's runserver command as the process will "halt" (until you quit out of IPython with Ctrl + D, or 'carry on' with continue) allowing you to use IPython's advanced tab completion, history, tracebacks and syntax highlighting for better object introspection and for jumping forward or backward through code, among other things (see IPython's documentation.)

Enjoy the post? You can follow me on twitter for more.