Question

The documentation of the Python readline module says "Availability: Unix". However, it doesn't appear to be available on OS X, although other modules marked as Unix are available. Here is what I'm using:

$ uname -a
Darwin greg.local 8.11.1 Darwin Kernel Version 8.11.1: Wed Oct 10 18:23:28 PDT 2007; root:xnu-792.25.20~1/RELEASE_I386 i386 i386
$ which python
/usr/bin/python
$ python
Python 2.3.5 (#1, Nov 26 2007, 09:16:55) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363) (+4864187)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
Traceback (most recent call last):
  File "", line 1, in ?
ImportError: No module named readline
>>> 

I have also installed Python 2.5 through MacPorts but readline is not available there either.

What can I do to provide readline functionality for Python's raw_input() function on OS X?

Was it helpful?

Solution

Have you tried to install the py-readline (or py25-readline for Python 2.5) port?

Also, in the snippet above, you are NOT using the MacPort python, but rather the Apple Python.

The MacPort version should be located in the /opt/local directory structure. You should check your path.

OTHER TIPS

Try rlwrap. It should work with any version of python and in general any shell.

Install via brew install rlwrap on Mac OS X

usage as rlwrap python. It stores history as well.

It's not shipped in OS X because of licensing issues (restrictions brought to you by the GPL).

Macports python should be fine if you have the readline package installed.

You should be able to get readline support on Mac's native Python. Apparently it's implemented via BSD's editline, and you have to start it up slightly differently. I just tested this on a Mac, and it works fine (OS X 10.5.7). See man python for more details, the section "Interactive Input Editing and History Substitution" - mirrored on the web here.

luga ~ $ python 
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import rlcompleter
>>> import readline
>>>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top