How I fell in love with debugging tools for python 2012-03-16

Until recently, I've never got to really use pdb (python debugger) nor to like it. It didn't bring me anything, broke my mental workflow, was annoying to use and print statement always seems more efficients.

Then I discovered:

ipython --pdb script.py

And my world changed.

What does it do? It simply drop you into ipython if your script crash at the exception raising point. So. Damn. Simple. and effective. Awesome shell and you don't have to modify your code and/or remember how the hell to set a breakpoint with pdb. You just have to change "python" into "ipython --pdb".

(If you don't know ipython, download it, install it and use it instead of the default python for your python shell sessions, it's a day and night chance, just the autocompletion on tab is a killer feature).

This damn simple trick has been life changing for me. Just test it and there is great chances that you'll fall in love.

Ipdb

Sometime you'll still need to use a pdb-like approach. But pdb seems so arid next to ipython. Lucky we are, someone got the awesome idea to "merge" ipython and pdb!

I present you: ipdb

Just drop:

import ipdb import set_trace; set_trace()

where you want in your code, just like with pdb.

And for lazy Vim users with as good memory than mine:

command! Ipdb norm! Ofrom ipdb import set_trace; set_trace()

(Then just use the command :Ipdb and this will put the desired line on the line above your cursor).

I want this in django!

Lucky you are again! This is nearly as awesome as the previous trick (but not as powerful as an ipython shell but still awesome).

Just install django-extensions and werkzeug (pip install django-extensions werkzeug) then run:

python manage.py runserver_plus

And on an exception you'll be dropped on werkzeug debugging page that allow you to browse you code and, way more awesome, to launch a python console right there in your browser on every part of the stack!

This is also very life changing.

Make sure to look at the others features of django-extensions, some are of it are very cool.

Bonus

Simple bash aliases that make me win so much time and make virtualenv way more enjoyable to use:

alias mkve="virtualenv --no-site-packages --distribute ve; source ve/bin/activate" alias ve="source ve/bin/activate"