To code or to pdb in Python

20 December 2010   6 comments   Python

Mind That Age!

This blog post is 7 years old! Most likely, it's content is outdated. Especially if it's technical.

Powered by Fusion×

To code or to pdb in Python This feels like a bit of a face-plant moment but I've never understood why anyone would use the code module when you can use the pdb when the pdb is like the code module but with less.

What you use it for is to create you own custom shell. Django does this nicely with it's shell management command. I often find myself doing this:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from this import that
>>> from module import something
>>> from db import connection
>>> con = connection('', bla=True)
>>> con.insert(something(that))

And there's certain things I almost always import (depending on the project). So use code to write your own little custom shell loader that imports all the stuff you need. Here's one I wrote real quick. Ultra useful time-saver:

#!/usr/bin/env python
import code, re
if __name__ == '__main__':
   from apps.main.models import *
   from mongokit import Connection
   from pymongo.objectid import InvalidId, ObjectId
   con = Connection()
   db = con.worklog
   print "AVAILABLE:"
   print '\n'.join(['\t%s'%x for x in locals().keys()
                    if re.findall('[A-Z]\w+|db|con', x)])
   print "Database available as 'db'"

This is working really well for me and saving me lots of time. Hopefully someone else finds it useful.


I think you mean, "why anyone would use the pdb module when you can use the code module" ?
Peter Bengtsson
No I did not mean that.

What I was trying to say was that I thought (note: past tense) that pdb was like code but better. Now I know (note: present tense) that code can be used for other cool stuff.
Craig Smith
That is very cool, and I also didn't know you could use the code module that way. In this specific case could you have used I've often dealt with the "must set up all this state" thing in the python command window by adding things to a project specific file, or using "python -i" to dump me into the command window with a bunch of code already run. Thanks for blogging this.
Why don't you use the *-i* flag of the intepreter. It fallbacks on the python intpreter when the code is done and with all the local variables availables in local scope.
Peter Bengtsson
The reason why? Because I didn't know about it.

Just tried it. It works! Thanks for the tip!
Luke Plant
I usually use IPython for this:

import IPython; IPython.embed()

I can't find an easy way to add local variables into that, although there are two features which make it not so much of an issue:

- great history mechanism
- 'run' command to run a script/module in the current environment.

I find I can no longer live with a bare Python shell after having being spoilt with IPython!
Thank you for posting a comment

Your email will never ever be published

Related posts

Page here about DoneCal 19 December 2010
Speed of DoneCal API (over 1,400 request/sec) and HTTPS (less than 100 request/sec) 27 December 2010
Highlighted code syntax in Keynote 30 August 2014
Cryptic errors when using django-nose 07 December 2011
type - Writing shell scripts 28 April 2006
Geeking with Eterm and Tkinter 09 February 2006