Snakes and Cats. Tab-completion in Python and iPython on Leopard
[UPDATE]: The latest builds of iPython have a fix for tab-completion built-in, so the patch mentioned below isn’t necessary.
Python users who are running Mac OS X 10.5, aka Leopard, will be pleased to find that Leopard ships with Python version 2.5.1, and comes with a host of wonderful things. Especially in the PyObjC realm, something I’ll cover in another post. Tiger had a rather lame build of Python 2.3 installed by default which had no readline support.
Readline is a gnu library which provides command-line editing, history, and tab-completion services, and it (or something like it) is pretty much essential for doing any real work in the python interpreter. One of the first things I’d do when setting up a new Tiger system is to install the pythonmac packages from pythonmac.org, which were compiled with that readline support.
On Leopard, Python does have command-line editing, courtesy of “editline”, which performs a mostly identical service to readline. However: Tab completion in the python interpreter isn’t working. (if you don’t know, tab-completion is a really handy feature where you type part of a command, hit ‘tab’, and the rest of the word you were trying to type is filled in. It saves you tons of typing and typing errors. With something like Python, you can use it to help you remember method names in classes:
>>> import os
>>> os.<hit tab key here>
Display all 235 possibilities? (y or n)
‘y’ will get you a big ol’ list of all os’s methods and attributes. It’s _really_ handy. But it weren’t workin’! What’s more, I often use iPython, and on Tiger somehow I got tab-completion for free (maybe because I’d built and installed readline?), but not on Leopard.
Happily, there are user communities for this kind of thing, and Python user communities are active and usually very helpful. No sooner was Leopard “out in the wild”, then folks on the pythonmac-SIG mailing list had a solution, which I will repeat here in my own weird convoluted way…
Here is the thread: http://mail.python.org/pipermail/pythonmac-sig/2007-October/019289.html
To get this to work with the python interpreter, you could simply add a line to a .pythonstartup file (and set your PYTHONSTARTUP environment variable). If you don’t already have a startup file, create one and set the PYTHONSTARTUP variable in your .profile or .bash_profile or whatever shell rc file you use. I use bash, so I added the following to my .bash_profile file:
# Special startup .rc file for python
Since my PYTHONSTARTUP file is named “.pythonstartup”, I open that file and add:
readline.parse_and_bind("bind ^I rl_complete")
Then I quit and relaunch my terminal (or ‘source .bash_profile’) to pick up the PYTHONSTARTUP environment change, and we have tab-completion in Python’s interpreter. Yay!
But what about iPython? So one way is to patch your local copy of iPython, although I suspect it will be patched fairly soon anyway. If you use iPython on Leopard, check out the link above (pythonmac-SIG mailing list). One of the members of that list posted a patch — I hope he doesn’t mind if I attach it here…iPython patch to fix tab-completion
If you don’t like the idea of patching your iPython install, just add the above 3 code lines to your .ipython/ipy_user_conf.py file, but keep in mind this is only for Leopard.
All in all, Leopard is going to be great for Python coding. The bridge support features with PyObjC are sooo cooool. Most of the really useful frameworks on Mac OS X are “bridged” so you can import them and start using them.