Python interactive mode history and arrow keys

Question:

I was wondering if anyone can explain why all of a sudden in Python interactive mode all arrow keys are failing?

When I press up button for example to go through command history I get “^[[A”. Same with any other arrow keys.

I have no idea why this happened and it was working before (on OS X Snow Leopard). Does anyone know a way to fix this?

Many thanks,

G

Asked By: givp

||

Answers:

This behaviour commonly shows when you do not have readline support. If you are using MacPorts, try port install readline, see if it will fix it. You can also see this page for some further explanations.

(Also useful to know: some programs do not use readline even if present on the system. You can force it on them by using rlwrap (port install rlwrap). For example: rlwrap ocaml -init code.ml will start up OCaml, read code.ml, and start REPL with readline support)

Answered By: Amadan

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.

Answered By: Ned Deily

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
Answered By: Mikael Lepistö

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)

Answered By: Hexatonic

The command

brew install readline

worked for me.

Answered By: chengguan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.