Welcome to Panela, Matt Harrison's take on mostly Open Source, Linux, Python, innovation in those areas, other buzzwords and Dick Proenneke. It comes complete with the illustrations as needed. Note the opinions expressed here are merely my opinions and not the opinions of my employer.

about Matt


««Dec 2010»»

Mailing List

My RSS Feeds

Python and emacs (5): pdb and emacs

posted 2009.08.13 Thu

pdb is the interactive debugger the ships with Python (much like gdb). Though I've heard that many prominent programmers do 90% of their debugging with the print statement, sometimes pdb is useful for that other 10%. (Besides that I'm coming to the realization that most production code shouldn't have print statements, and if you are using print to debug, you should be using logging instead, but I digress.)

pdb works from the command line like a charm. So why integrate it with emacs instead of just running it from a terminal? Or if you live in emacs, from a terminal inside emacs? For me the main reason is to have another window that shows me where I am in the debugging process. Sure pdb has the w(here) command that prints out some lines before and after, but it's so much nicer to have your highlighted code being tracked automagically for you. See that little triangle in the screenshot above? So what's the problem with running from within emacs? Out of the box (gentoo, python 2.5, emacs 22-23) integration with pdb doesn't work for me. There appears to be a few ways of getting pdb to work under emacs. I have my own.

I needed to apply this patch and I also need to get around an emacs nicety that turns into an annoyance for me. See I put all my test files under a test directory that sits alongside my main module/package (also the same directory that setup.py is in). If I have test data I usually reference it using a relative path from that base directory and not the directory that the test code lives in (usually a level down). But by default whenever emacs enter gud mode it kindly changes to the directory that the file lives in when you invoke pdb. Rather than delving into emacs, I patched my pdb again to add --cwd support (similar to nose's -w). The result is here (for 2.5). For 2.6 I've written a wrapper (mypdb.py)(since the aforementioned patch is applied) that gives me my desired -w/--cwd support.

To run pdb with these scripts, make sure they are executable and in the PATH. Then type M-x pdb, when it asks for the command to run type pdb --cwd /current/directory/you/want/to/use /file/to/debug.py (make sure pdb points to the 2.5 pdb I provided or change it to mypdb.py for 2.6). Cheers!


links: digg this    del.icio.us    reddit

1. Keegan left...
2009.08.14 Fri 2:22 am

What colour theme are you using?

2. Giulio Piancastelli left...
2009.08.14 Fri 4:45 am :: http://gpiancastelli.wordpress.com

Keegan, see http://panela.blog-city.com/python_and_emacs_2_color_themes.htm