Debugging tools for Python & Django

  • Jan

Tools that will make your life easier

Why should we use a debbuging tool?

Well, the answer is simple, it will save us a lot of time. I used to put flags in my code, which it worked, most of the times. If I would've known these tools my life would've been so easier. In the following section I will show you a couple of tools mostly for python and how to use them with django.



PDB, like the official page says, it's an interactive source code debugger for Python programs, and IPDB is a tool that works over PDB that brings features like tab completion, syntax highlighting, better tracebacks.

To start using it right away, install it using pip:

$ pip install ipdb

Then, instead of using your regular flags:

# buggy code
print "What I'm I doing"
# More buggy code

Try this:

# buggy code
import ipdb; ipdb.set_trace()
# More buggy code

This will also work in frameworks like Flask and Django.

You can also run pdb for a specific python program:

$ python -m pdb

Here is an example of how your terminal might look:


Most used commands:

  • h(elp)

    Print the list of available commands. You can also pass a command as an argument, it will print the help about that command.


  • w(here)

    Print a stack trace, with the most recent frame at the bottom. An arrow indicates the current frame, which determines the context of most commands. E.g.:

  • d(own)

    Move the current frame one level down in the stack trace.

  • u(p)

    Move the current frame one level up in the stack trace.

  • s(tep)

    Execute the instruction, if the current instruction is a call to a function, it will call the first instruction of the function, and so on.

  • n(ext)

    Very similar to step, but if the current instruction to execute is a method, it will execute it without going inside of it.

  • c(ont(inue))

    Continue execution, it will only stop when a breakpoint is encountered. You can place breakpoints anywhere in your code, you will find how in the command break

  • r(eturn)

    It will Continue execution until the current function returns.

  • b(reak)

    It will set a break given a current line number as an argument, also the line number may be prefixed with a filename and a colon, to specify a breakpoint in another file.

  • l(ist)

    List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. While with one argument, will list 11 lines around the specified line number, and with two arguments, list the given range.

  • a(rgs)

    Print the argument list and their values of the current function.

  • p

    Evaluate the expression in the current context and print its value. Similar as print.

  • q(uit)

    The program or code being executed is aborted.



PuDB is a tool very similar to pdb, actually you can even do exactly the same with both, the difference is that PuDB comes with a GUI, which could simplify things for you.

The usage is pretty much like pdb, you just have to install via pip.

$pip install pudb

Once you have it installed, you just have to call set_trace in your code.

import pudb; pudb.set_trace()

Or if you want to run a whole program you could do this in your terminal:


This is how the GUI looks

As you can see in the image above, the variables, stacks and breakpoints automatically changes while you are executing your code, you can also visualize the breakpoints in the left panel, which it could be useful. I personally didn't like the default theme, but the good thing is that you can choose over 3 other themes, you can even create your own theme. The user interface is pretty easy to use.

A couple of PuDB commands:

  • SHIFT + ?

    Popup with all the available commands.

  • CTRL + p

    It will allow you to change the appearance and behavior of PuDB

  • SHIFT !

  • c

    Continue the execution until the end, or until it finds a breakpoint

  • n

    Run the next instruction

  • It Swaps you between the terminal and the panel.

  • b

    Creates a breakpoint in the highlighted line

  • H

    Moves the cursor to the next line to be executed

  • u

    Moves you one level up in the stack trace

  • d

    Moves you one level down in the stack trace

  • j/k - up/down

    Moves you up and down in the code panel.

  • q

    Quit the current execution of the code

Both PuDB and IPDB works in python and anything that runs python.


A Django traceback page with benefits.

If you are a Django developer, this app could be part or your useful tools when you are debugging. It comes with three main features:

  • Provides a nice access view to the source code.

  • AJAX based debugger

  • Cleaner GUI

To make use of this you simply have to install the following packages:

$ pip install django_extensions werkzeug

Add django_extensions to your INSTALLED_APPS

And finally run:

$ python runserver_plus

Now everytime an exception occurs, it will bring the new traceback page. You will see something like this:

You have two options when hovering a particular line, like the bellow image show:

The first option displays the source underneath the traceback, which it could save you some time:

The second option opens the Interactive Debugging Console as show bellow:

The insteresting part of the last option, is that you could check the context in any particular line, which I believe is awesome!. 

Django Debug Toolbar

Last but not least this amazing tool, will save you a lot of time. Not only works for debugging, but also, will help you optimize your code, specially with those tedious queries!.

Django Debug Toolbar as the official page says is a configurable set of panels that display various debug information about the current request/response, and one of the main feaures is the capability to show you the queries after every request.

For the instalation, I recommend to go to the docummentation since it may change often between versions : Set Up

This is how it currently looks:



Even though sometimes it's "easier" to use flags as a debugging option, it's not always the best way to solve problems. These tools are meant to be used, and they will help you most of the times to solve issues quicker than without them, so give them a chance, you might like them.

Posted on Jan. 3, 2015, 9:59 p.m.