Making the Mac's Python more friendly

27 Sep 2007

If you’ve tried using the default Python interpreter on the Mac for more than a moment you’ll have become immediately frustrated by the lack of readline functionality – you can’t edit the command you’re typing or access your command history. It’s enough to reduce a grown person to tears. And indeed me.

If so, you’ll definitely need this page, which tells you how to fix it. I thought I’d blogged this already, but apparently not – I was trying to fix my iMac at home today so I could get some work done on it.

Once you’ve done this you can also do the following to allow you to tab complete things like internal functions, variables, and class and object member names. First you want to create a python startup file if you’ve not already. You do this by creating a python file and setting the environment variable PYTHONSTARTUP to the name of this file. From now on python will load that file every time it starts. In this file I’ve got:

try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

This loads the readline module and sets tab completion going. Now you’re ready to really get going!

Whilst on this topic, you might also want to check out ipython, which is a python interpreter with extra bells and whistles (and not Mac specific). Q pointed me to a good set of tutorial videos on ipython, and a quick start video, which I’m currently plowing through.