Debugging in python with pdb and dropping into a ‘proper’ session

If you code in python, there’s a reasonably high chance you’ve heard of dropping into a debugger with something like pdb, or if you prefer ipython, ipdb.

It’s a really, really handy skill, and if you haven’t done this before, this 40 minute video gives a good overview of how it can help you:

But what if I want to do stuff on more than one line?

One nice thing about the debugger like this is that it puts you in a REPL like environment, where you can see what the values of various variables are, and call functions to see what they returned values might be.

The thing is, if you want to use a loop, or an if/then construct, you’ll get a stroppy message like so, when you try to break onto the next line:

for obj in collection_of_objects:
*** SyntaxError: unexpected EOF while parsing

This isn’t great – if you’re trying to investigate why code is working the way it’s working, having some basic features normally offered  by the language makes life much easier – part of the point of dropping into a debugger in the first place is to use the language you’ve written code in to understand the what’s going on in the code better.

Oh, interact – that’s what it’s for

This is where interact comes in. if you’re in a pdb session, you can type interact, and  you’ll end up in a proper python session where you have all the abilities you’d have with a regular python prompt, like loops, if/thens and so on.

Once you’re done, hit ctrl+d and you’ll end up back in the debugging session you were in when you first started with pdb.

I’ve been programming python professionally since 2008, and I discovered this today. Oh well, better late than never, right?

Handy snippets – Python without the .pyc cruft

I maintain a huge workflowy file of snippets and tricks and, I suspect some of them will be handy for others to use too. I’m going to sharing them regularly, when I’m not sure what to write, mainly to keep the habit of blogging, until it comes naturally.

Part of my professional life, involves me writing a fair amount of python code. When you write python, you end up creating lots of files ending with .py, but when you run the code, you end up with compiled python files, sending in pyc.

If you don’t want loads of pyc files crufting up your workspace, and you’re workignon the command line, there’s a handy little environment variable you can set in your shell, to banish those ugly files once and for good. Run this command before working (or better yet, make it run whenever you open a terminal window):


See, isn’t that better?

I picked up this gem from the Hitchhiker’s Guide to Python, a free, opinionated book from Kenneth Reitz about good practices with working with Python. Reading it will almost certainly improve your python – I know mine is better as a result of spending some looking over it.