Python tab completion in OS X

2010 January 6
by Steven

I’ve just got a new Mac Book and am currently going through the usual palaver of installing the varied array of software I use on a daily basis.

My first tip from this experience: how to set up tab completion in the python interpreter that comes with OS X Snow Leopard (10.6).

Normally all you have to do is put:

import rlcompleter
import readline
readline.parse_and_bind("tab: complete")
in the file specified in your $PYTHONSTARTUP environment variable.

However, that wasn’t working for me. A quick Google revealed a solution here. However, that page is a little out-of-date and, thankfully, you no longer need to go through the rigmarole described there for Python 2.6. It turns out the readline module shipped with OS X has less features than the ‘proper’ GNU version.

All you need to do (as well as putting the code above in your $PYTHONSTARTUP file) is type this in the terminal:

sudo easy_install readline
and you’ll have tab completion.

All this also applies if the Python interpreter supplied with your Linux distribution isn’t doing tab completion, etc. If you don’t have easy_install, then you can probably install the python readline module using your usual package manager.

One Response leave one →
  1. March 26, 2010

    What about iPython?

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS