Skip to content

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.

4 Responses leave one →
  1. March 26, 2010

    What about iPython?

  2. Fritz permalink
    February 11, 2011

    Thanks! that saved me a ton of time…

  3. March 10, 2011

    In OS X 10.6.6, even if you pip/easy_install readline, which has that capability, the system file readline.so takes precedence over the installed one.

    To fix this you need to

    cd /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/ mv readline.so readline.so.default sudo cp /Library/Python/2.6/site-packages/readline.so .

    and it works.

    Now, not sure if a future system upgrade will overwrite this, but apple usually upgrades python stuff in major revisions. Should safe ’till Lion.

  4. Steven permalink*
    March 10, 2011

    Thanks for the update Panos!

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