Notice: I have neither posted nor updated any content on this blog since the mid 2010's. (😱) Please check out the homepage and my bio, for more recent information.

pdb: Using the Python debugger in Django

A couple conversations I had at DjangoCon last week reminded me that I’d never gotten around to finishing this little post.

Since I started taking my first iOS development baby steps last year, I’ve become quite dependent on breakpoints and stepping as part of my Objective-C debugging workflow.

…And yet, in Python/Django — the stack I spend most of my time working with — I’d stuck to the poor habit of throwing print statements around any found exceptions — for example, to figure out what lies in request.POST for a Django view. And then I came across and started tinkering with pdb.

Here’s a brief overview on how to rock your Python debugging workflow by using an actual interactive debugger. The example below highlights the most basic of usecases, but it’s a good start.

Using pdb with Django

Say, you have a view that looks like this:

from django.http import HttpResponse

def default(request):
    
    # completely innocuous variables
    foo = 1
    bar = 0
    
    # completely innocuous division
    ni = foo/bar
    
    return HttpResponse("Foo says %d" % ni, mimetype="text/plain")

And say, you run your local development server with:

manage.py runserver

When accessed, this view will generally throw you something like this:

thumbnail of error example


By replacing the manage.py runserver command with one wrapped in PDB, we can start digging a little bit deeper into this problem view.

python -m pdb manage.py runserver

You will notice that the shell hangs on a (Pdb) prompt. Enter c for “continue” (command reference here) and hit enter. (You’ll also notice that control-c restarts runserver, since the default PDB behavior is to restart a script after it ends. Just hit control-c a few times to break PDB’s execution loop when you want to kill the server.)

You’ll notice that running under PDB doesn’t do anything by itself.


You’ll need to set a breakpoint to actually tell PDB where to stop execution. Inserting the following line of code causes PDB to break when it executes — which, in turn, triggers the debugging shell.

import pdb; pdb.set_trace()

In our example, we’d like to inspect where our exception is getting thrown, so we’ll throw it right before the offending line of code:

from django.http import HttpResponse

def default(request):
    
    # completely innocuous variables
    foo = 1
    bar = 0
    
    # well, this is where our error is, so let's trace it
    import pdb; pdb.set_trace()
    ni = foo/bar
    
    return HttpResponse("Foo says %d" % ni, mimetype="text/plain")

Congrats! Your browser is now waiting for your view to finish, your view is waiting (at the breakpoint) for PDB to finish, and PDB is waiting for you in the shell.

Doing things in PDB

At this point, the PDB shell acts like the normal python/ipython shell. You can look at variables right in the scope of the breakpoint.

thumbnail of debugging example

When working with a breakpoint, you can even screw around with local variables to try and find a fix, but note that only the last line you execute is remembered when you continue. You can still execute several statements by using the semicolon:

thumbnail of debugging example

Other neat things to try:

  • If you’re working with utility functions that can be called from various places, you can use the where command get a full trace of how Python got to that point in your script. This is pretty much exactly as advertised.
  • In addition, the Python globals() and locals() functions (which spit out all global or local variables, respectively) can be used within this (Pdb) prompt, which can be nifty if you’re debugging a GET/POST/COOKIES/SESSION issue.

I can’t say that I’ve been using PDB nearly as often as I’d like, but I have found it useful for the occasional mis-assigned variable or other super enigmatic bug.

Hope someone else finds it nearly as helpful as I have.

Trails at Mount Spokane State Park

Cheated a bit with multiple exposures and Photomatix for HDR goodness, but hey, what can you do.

(509)

Just over 26 months ago, I blindly moved to Spokane for a summer. I knew nobody in the area, I knew nothing about the place, and all I knew was I'd secured an internship and a small studio apartment for three months.

On my connecting flight from Phoenix, I sat next to a girl whose father lived in Northern Idaho and whose mother lived somewhere in Texas. She asked me whether I was from the area or not, and I told her that no, I was just moving in and playing it by ear and I really wasn’t sure what I was getting myself into.

I only remember bits and pieces and I don’t remember exactly what she said except for how it was a “different sort of city” (and I’ve since heard this from others, too) and that it’s a beautiful part of the country and that I’d probably like it and it’s hard to leave once you’re used to the place.

I’ve been back every summer since. (Which is to say, two.)

And just over a week ago, I became the newest full-time member of the Spokesman-Review newsroom.

So, I guess I liked it.

Just the thought of permanence — anywhere, in anything, so soon after college — is dizzying to me… But hell, this was one of those opportunities that I couldn’t pass up:

I get to work on a pretty badass news site, dabble in iPhone/iPad development, I have decent creative control over what I work on, and hey — I have a real job right out of school. That’s as good as it gets, if you ask me.

(But check back once I get myself into one of these infamous Spokane winters. It’s La Niña this year, apparently — I’ve been warned.)

Excited and scared don’t begin to describe it. That this is for real now crosses my mind every so often. And — just like the start — I feel like I’m flying blind again; but this time, into the whole real world thing.

It’s going to be great.

June

Random scenes from the massive amount of driving I did in June. Nearly 4,000 miles that took me from New York City and the Jersey Shore to as far west as Central Washington.

Somewhere on the Jersey Shore Loveland Pass, Colorado Welcome to Utah San Rafael Swell, Utah San Rafael Swell, Utah Rural Utah Driving through the Bonneville Salt Flats, Utah Bonneville Salt Flats, Utah Perrine Bridge, Twin Falls, Idaho

The New Who Thing

Those are relatively minor quibbles though. My biggest complaint, by far, has bothered me for some time but has taken me only until recently to put my finger on. Tumblr discourages identity. Or, to be more specific, it promotes shallow identity. Moreso than other blogging systems like WordPress or ExpressionEngine, Tumblr blogs frequently offer only scant few details about their authors. I can’t recall how many Tumblr sites I’ve visited where it wasn’t clear who was behind the posts, what their background was, or what their intent was. Many of these sites are artful, well designed and are actually quite engaging, but I guess I’m old fashioned in that I like to know who’s behind them.