Python interactive mode history and arrow keys

I finally got this working. I just had to install readline with easy_install and cursors and backspace started magically working.

sudo /opt/local/bin/easy_install-2.5 readline

You don't say which Python you are using but the symptoms you mention are indeed usually caused by Python not being built with readline support. These days Python on OS X can be built to use either the GNU readline library or the Apple-supplied editline library (AKA libedit). You can use the following two commands to show exactly which Python you are using. If that does not help you figure out what is going on, edit your question to show the output from those commands.

Here's an example that shows a recent MacPorts Python 2.6 on OS X 10.6:

$ python -c 'import sys;print(sys.version);print(sys.executable)'
2.6.5 (r265:79063, Jul 15 2010, 01:53:46) 
[GCC 4.2.1 (Apple Inc. build 5659)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python

$ otool -L $(python -c 'import readline; print(readline.__file__)')
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/readline.so:
    /opt/local/lib/libreadline.6.1.dylib (compatibility version 6.0.0, current version 6.1.0)
    /opt/local/lib/libncursesw.5.dylib (compatibility version 5.0.0, current version 5.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)

The path prefix /opt/local/ is the default location for MacPorts-installed software and the output from otool indicates that this Python's readline module is dynamically linked to the MacPorts-installed GNU readline library.


If you are using homebrew, this is an easy fix:

brew uninstall python
brew uninstall readline
brew install readline  --universal
brew install python

That fixed it for me (running OS X Mavericks 10.9.5)

Tags:

Python

Macos