python tab completion Mac OSX 10.7 (Lion) python tab completion Mac OSX 10.7 (Lion) python python

python tab completion Mac OSX 10.7 (Lion)


Apple does not ship GNU readline with OS X. It does ship BSD libedit which includes a readline compatibility interface. The system Pythons shipped by Apple and the 64-bit/32-bit Pythons from python.org installers are built with libedit. The problem is that the commands supported by libedit are completely different from those of readline (see for example the discussion here). The traditional 32-bit-only python.org installers do use GNU readline as do some other 3rd-party distributors of Python for OS X, like MacPorts. Chances are that you were previously using such a Python and not a recent Apple one. You do have a few options, besides modifying Django: you can install the third-party replacement readline module; or you can use another Python that comes with GNU readline. However, you should not use the python.org 32-bit-only Pythons on 10.7 because, unfortunately, Xcode 4 on 10.7 no longer includes gcc-4.0 and the OS X 10.4u SDK which those Pythons need to build and install packages with C extension modules.

Putting the following in the python startup file will enable tab completion for both the libedit interface and the typical readline module. For more information on the python startup file, see here

import readlineimport rlcompleterif 'libedit' in readline.__doc__:    readline.parse_and_bind("bind ^I rl_complete")else:    readline.parse_and_bind("tab: complete")


As it uses libedit/editline, the syntax to enable autocompletion is a little bit different.You can first force emacs bindings (as it is with readline if I'm not wrong) by typing :

readline.parse_and_bind("bind -e")

Then you can add autocompletion linked to your TAB button (man editrc) :

readline.parse_and_bind("bind '\t' rl_complete")

And if you want to support indenting and has a history (found on internet), it should look like that (unless I made a mistake) :

import readline,rlcompleter### Indentingclass TabCompleter(rlcompleter.Completer):    """Completer that supports indenting"""    def complete(self, text, state):        if not text:            return ('    ', None)[state]        else:            return rlcompleter.Completer.complete(self, text, state)readline.set_completer(TabCompleter().complete)### Add autocompletionif 'libedit' in readline.__doc__:    readline.parse_and_bind("bind -e")    readline.parse_and_bind("bind '\t' rl_complete")else:    readline.parse_and_bind("tab: complete")### Add historyimport oshistfile = os.path.join(os.environ["HOME"], ".pyhist")try:    readline.read_history_file(histfile)except IOError:    passimport atexitatexit.register(readline.write_history_file, histfile)del histfile