Subscribe to Who needs shades of grey?        RSS Feed

ipdb freely

Icon Leave Comment
You know, since starting using Django, there have been a few occasions when I've had to get up and drag someone over to the screen to show them how awesome it is. There's nobody around here tonight. It's a Sunday, I'm at home and I'm enthusiastically bouncy and and the same time slapping my forehead for not thinking something so blindingly obvious was available.

I've been debugging django using a combination of the excellent django-debug-toolbar and good old-fashioned logging to file combined with a couple of terminals running something along the lines of tail -f myproject.log | grep <keyword>, which is how I always used to do it in PHP. I'd dismissed the python debugger, pdb, because I wasn't running a standalone app. But of course, with the django development server, I am, and pdb is most definitely available.
If you haven't used it, it's just like any other command-line debugger, with commands to step into or over lines, inspect variables and print out a back trace. But it's more than that. With ipdb, for instance, which is usually just a pip install ipdb away, you get syntax highlighting and decent command history. It supports aliases (macros) which can be loaded from its configuration file to make things super-easy.

If you start the django development server with python runserver then it's as easy as having
import pdb 
# or import ipdb as pdb

where you want your breakpoint. Of course, you can do more. But this'll get you into a debug console in the terminal where you started the development server. From there, help will give you everything you need to know.
There's one tiny glitch, which has apparently been patched, which is that if you change any current-level variable you won't be able to see the changes until the current function returns:
ipdb> myvar
ipdb> myvar = 'goodbye'
ipdb> myvar

whereas further down the pipe things behave as you'd have expected:
ipdb> myvar['test']
ipdb> myvar['test'] = 'goodbye'
ipdb> myvar['test']

I'd previously played around with PHP's xdebug, which I had hopes for, but since it crashed apache on every machine I tried it on, I didn't bother my head further.

I'm just annoyed that it took me this long to realise that I could use the debugger against web requests.
You know how useful this is? The site I'm developing uses facebook connect. If I am not bang in the middle of an authenticated session, I _cannot_ make half the facebook requests I need to continue debugging. I have to either change the source and reload the page each time, which is a very shotgun technique, or I have to run the code offline with fake facebook return data.

Well, not anymore. Now I can interact with facebook right in the middle of everything. Woot.

0 Comments On This Entry


November 2018

18192021 22 2324

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)