28

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

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
givp
  • 2,494
  • 6
  • 31
  • 30

5 Answers5

15

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)

Hexatonic
  • 2,251
  • 2
  • 21
  • 26
  • 2
    I just got readline working in python3 for OSX 10.10.1. I had to change the last line to `brew install python --readline --framework` and then I had to do the following: (1) Add `/usr/local/share/python` to my PATH. (2) `pip3.4 install readline`. Then I typed `python3` and readline was working again. – Ben Kovitz Jan 26 '15 at 11:57
  • is this going to replace system Python 2.7.10 with some other version of Python 2.7? – user5359531 Dec 12 '16 at 04:11
  • @user5359531 system python won't be replaced, but if you've already `brew install python` then you follow this procedure, brew may replace _its_ version with a newer one. Check: `which python`, (reports brew's version is at /usr/local/bin/python), `brew unlink python && which python` reports system version, `brew link python` to reinstate the brew version. It's non destructive. – ptim Jan 17 '17 at 14:14
  • Worked for me too. I am on OS X 10.12 – Jeune Apr 20 '17 at 18:56
13

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
Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
8

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.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • The hint to look at the Python readline module was helpful. Apparently mine was in fact *not* linked to `libreadline`. I don't know why not, though since readline was installed just fine. Anyway, `port -f uninstall py27-readline; port install py27-readline` fixed it. – Celada Mar 09 '13 at 16:47
5

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)

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 1
    tried installing readline but it didn't help. Thanks anyway. Will keep digging. – givp Aug 14 '10 at 14:32
  • It worked for me as well. To get it to work with python's pdb, install rlwrap (`port install rlwrap`), then launch python with the command `rlwrap python`. See the man pages for rlwrap for passing arguments, etc. – Dannid Dec 09 '13 at 20:17
0

The command

brew install readline

worked for me.

Marcus
  • 2,153
  • 2
  • 13
  • 21
chengguan
  • 21
  • 3
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30028348) – Beso Oct 08 '21 at 15:28