28

I'm using zsh, iTerm2 (3.0.9), and pyenv (1.0.2) with pyenv global set to 3.5.2.

In the Python shell, the up and down arrow keys used to work, to access the previous commands in the history. But now after upgrading to OSX 10.12, instead it shows control characters. For example up arrow displays:

^[[A

I've tried installing readline as suggested in Seeing escape characters when pressing the arrow keys in python shell but that didn't help. I don't have the PYTHONSTARTUP variable but didn't used to before, and not sure how that interacts with pyenv.

Community
  • 1
  • 1
danib
  • 441
  • 5
  • 8
  • Same happened to me, but I'm using the built-in bash, terminal, no environments, didn't upgrade the OS and still happened. – Petruza Feb 03 '19 at 20:53

7 Answers7

12

I had the exact same issue and this command worked for me easy_install -a readline.

Full credit here: ipython complaining about readline

Community
  • 1
  • 1
joanfihu
  • 336
  • 2
  • 9
  • 2
    This fixes it for Python 3.5.2, but using pyenv to switch to Python 2.7.12, arrow keys still not functioning in that shell. – danib Oct 09 '16 at 23:11
  • Thanks! my comment is buried over there, and this Q's title is bang on, so I thought I'd note that `easy_install -a readline` fixed my global python, but I needed to recreate my virtualenv to fix the issue in there. – ptim Jan 17 '17 at 14:31
  • 1
    This did not fix it for me for Python3. – edA-qa mort-ora-y Jan 28 '17 at 17:45
  • Fixed for me, Python 3.8.1 via pyenv. Had to simply do: `pip install readline`. – Kamil Dec 12 '20 at 15:49
12

I solved it by installing python from homebrew:

brew unlink python
brew install python
jsphpl
  • 4,800
  • 3
  • 24
  • 27
3

This worked for me:

CFLAGS="-I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include" \
LDFLAGS="-L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib" \
PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2 \
pyenv install -v 2.7.11

Source: https://medium.com/@pimterry/setting-up-pyenv-on-os-x-with-homebrew-56c7541fd331#.urbdkrc9l

Joe Inner
  • 1,450
  • 1
  • 10
  • 14
2

This can happen with upgrading XCode or XCode tools (which often occurs with an OS upgrade) when using brew installed python.

In this case, you just need to reinstall:

brew reinstall python

If you need python 2, remember brew defaults to python3 now, so you need:

brew reinstall python2
Zim
  • 410
  • 4
  • 13
0

I'm seeing the same thing and the only "fix" I was able to come up with was to not run the pyenv init - command in my .zshrc file. That however will inhibit the functioning of virtual environments.. and so it's not a fix but a workaround to get the python shell history to work again.

I'm continuing to look and see if there is a permanent fix as I'm not nearly as productive without it.

Jake H
  • 19
  • 2
  • After trying the above fix about installing readline I can confirm it works for my python 2.7.8 env. Thanks! – Jake H Oct 13 '16 at 21:06
0

What solved it for me was running the steps mentioned by jsphpl (unlink and install) and then additionally using brew to link python again and allowing the overwrite of conflicting files.

kevb
  • 1
  • 1
  • 1
    This isn't really enough detail for an answer. Can you explain further how you went about "using brew to link python again and allowing the overwrite of conflicting files"? – mypetlion Mar 06 '19 at 19:41
-1

Need to configure python's shortcuts.

In shell, run idle3, then update the key shortucts:

How to repeat last command in python interpreter shell?

Roman
  • 8,826
  • 10
  • 63
  • 103
  • This answer (and link) only affect the shell as accessed by the idle interpreter, not the command line bash/zsh/etc shell. – Zim Jul 01 '19 at 17:33