239

In shells like the interactive python shell, you can usually use the arrow keys to move around in the current line or get previous commands (with arrow-up) etc.

But after I ssh into another machine and start python there, I get sessions like:

>>> import os 
>>> ^[[A    

where the last character comes from arrow-up. Or, using arrow-left:

>>> impor^[[D

How can I fix this?

In the regular bash, arrow keys work fine. The weird behavior is just in the interactive python (or perl etc.) shell.

5
  • I think this belongs on server-fault. This is caused by an incorrect terminal type.
    – epochwolf
    May 21, 2009 at 14:56
  • I agree with cartman below that it's a readline issue, not a terminal type issue.
    – Frank
    May 21, 2009 at 15:10
  • 2
    easy_install readline and then easy_install ipython works perfect in mac.
    – gnuyoga
    Oct 12, 2013 at 7:52
  • install anaconda2 or anaconda3 and set .pystartup please see link
    – Leon WANG
    Jan 7, 2018 at 23:58
  • 2
    @LeonWANG gives a bad answer - don't install something as huge as anaconda just to get your .pystartup file working. Completely unnecessary.
    – jdogg
    Oct 16, 2018 at 15:44

25 Answers 25

137

I've solved this issue by installing readline package:

pip install readline
16
  • 34
    I had to install libncurses-dev on my Ubuntu machine, then readline installed correctly.
    – Amir Eldor
    Dec 22, 2015 at 19:23
  • 8
    Installed ncurses-devel on CentOS and then readline installed no problem. Interactive shell is now working.
    – one.time
    Apr 7, 2016 at 15:26
  • 4
    can't pip install readline on my OS X machine, fails every time even after successfully doing brew install readline Dec 12, 2016 at 4:03
  • 29
    I had to install gnureadline instead, see stackoverflow.com/q/43013060/2846923 Nov 5, 2018 at 21:37
  • 6
    @TheGuywithTheHat: same issue over here. I tried to install readline on a Raspberry Pi running sudo pip3 install readline but when I opened the Python command prompt and typed a command, Python crashed. I had to uninstall readline and install gnureadline instead. Now it's working flawlessly... +1 for your advice!
    – GeertVc
    Jan 27, 2019 at 8:49
86

On OS X, I have different problem.

When I using system python shell, the keys is no problem, but problem in virtualenv. I'd try to reinstall/upgrade virtualenv/readline and nothing fixed.

While I try to import readline in problem python shell, get this error message:

ImportError: dlopen(/Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so
Reason: image not found

Cause there is /usr/local/opt/readline/lib/libreadline.7.dylib but not libreadline.6.dylib, so I make a symbol link:

ln -s libreadline.7.dylib libreadline.6.dylib

Problem has been solved!

10
  • After looking and looking I was able to fix the issue with this suggestion. Thanks!
    – gl051
    Jan 18, 2017 at 0:50
  • 1
    Won't this cause a problem if something calls v6, not realising that it's actually v7? Feb 24, 2017 at 16:05
  • 4
    I had the same problem, but instead of these instructions, I did brew update && brew upgrade. Whether this broke PHP in the process remains as yet to be seen. Feb 24, 2017 at 16:38
  • @AdamBarnes This also did the trick for me. I use pyenv as well, so made sure to uninstall/reinstall the python versions that I needed after upgrading all the homebrew packages.
    – funseiki
    Oct 11, 2017 at 18:22
  • 3
    I didn't have libreadline.7.dylib (perhaps because I'm on Mojave 10.14.x and then updated XCode 10.2.1) but had ver 8 instead. Executing ln -s /usr/local/opt/readline/lib/libreadline.8.dylib /usr/local/opt/readline/lib/libreadline.7.dylib fixed the problem of scan codes, but didn't bring back history command recall. Although in this case, half a fix is better than none. (Yes, I also added a link for 6, no difference). Addendum: this was/is only a problem with python2 for me. python3 worked fine without the links.
    – Zim
    Jun 27, 2019 at 19:03
53

On OS X, Xcode updates sometimes break readline. Solution:

brew uninstall readline
brew upgrade python3
brew install readline
pip3 install readline

If the problem still persists, try to remove readline using pip and install it using easy_install:

pip3 uninstall readline
easy_install readline
7
  • is there an equivalent if you are using system Python 2.7.10? Dec 12, 2016 at 4:02
  • 3
    @user5359531 I've updated the answer with an easy_install option
    – Max Malysh
    Dec 15, 2016 at 0:00
  • OSX 10.12 users also: stackoverflow.com/questions/7375545/…
    – ptim
    Jan 17, 2017 at 14:35
  • 1
    Using easy_install works for me, while the one with pip doesn't work.
    – justhalf
    Jun 26, 2017 at 5:39
  • 1
    This combined with the brew link readline (even though it was already linked) fixed it for me. Dec 8, 2017 at 17:37
45

On OS X, using python 3.5 and virtualenv

$ pip install gnureadline

In the interpreter do:

import gnureadline

Now arrow keys should work properly.


Additional information...

Note that as of Oct 1, 2015 - readline has been DEPRECATED (source https://github.com/ludwigschwardt/python-readline)

Use gnureadline instead (see: https://github.com/ludwigschwardt/python-gnureadline)

If I install readline instead of gnureadline using python 3.5, I receive errors after attempt to import in the interpreter:

>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
  Referenced from: /Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so
  Reason: image not found
3
  • this works, but it means I need to add import gnureadline at the top of every interactive session which is really annoying. Is there a way to kick this off at the beginning of every interactive session? Mar 28, 2019 at 14:55
  • 1
    @Panchishin try to use the PYTHONSTARTUP environment variable to name a file that has Python commands executed before the interactive prompt is displayed (i.e. import gnureadline). For possible other ideas also see: run python command line interpreter with imports loaded automatically
    – user12345
    Apr 1, 2019 at 23:58
  • This is the most up-to-date answer for this question. Feb 20 at 2:03
40

Looks like readline is not enabled. Check if PYTHONSTARTUP variable is defined, for me it points to /etc/pythonstart and that file is executed by the python process before going interactive, which setups readline/history handling.

Thanks to @chown here is the docs on this: http://docs.python.org/2/tutorial/interactive.html

7
  • Thanks, I agree that readline seems to be the issue. The system does have /usr/lib/libreadline.so.5 though. There is no /etc/pythonstart.
    – Frank
    May 21, 2009 at 14:35
  • 1
    After some googling it seems python on that system might have to be recompiled, after installing readline-devel.
    – Frank
    May 21, 2009 at 14:49
  • 1
    The environment variable is PYTHONSTARTUP, not PYTHONSTART. Not sure about the distribution-specific /etc/pythonstart file.
    – Ned Deily
    Dec 16, 2011 at 18:04
  • 1
    Actually, reading docs.python.org/2/tutorial/interactive.html gives all the info needed for a pystartup file.
    – chown
    Jan 21, 2013 at 19:08
  • 7
    On OSX with brew, all I had to do was brew reinstall python3 Sep 16, 2017 at 4:01
28

I have run into this issue recently and after reading a lot about pip install readline (does not work for mac osx) and pip install gnureadline and not being satisfied, this is now my setup which enables using arrow keys in any python console:

  1. install gnureadline using pip install gnureadline

now you can either do import gnureadline and arrow keys should work as expected. To make them work automatically follow the following steps:

  1. create (or append to) file ~/.startup.py: import gnureadline
  2. append to file ~/.bash_profile: export PYTHONSTARTUP=~/.startup.py

One thing that does not work, but did in my previous setup is: automatic import of gnureadline on pdb.set_trace(). If anyone has a good solution to this problem I would be grateful for a comment.

0
22

I had problems with shell history(tab/arrows commands) of Python 3.6.x on Ubuntu 16.04 LTS.

Python 3.6.x was installed from source.

What solved for me was install the module "gnureadline" as said by user12345, using this command line:

sudo pip3.6 install gnureadline

:)

3
  • 1
    I tried all other solutions but this one worked for me for my Python 3.6
    – Umair Ayub
    Jan 29, 2019 at 7:41
  • 2
    make sure to call import gnureadline in the shell for it to work Mar 11, 2019 at 13:32
  • 4
    No need to use sudo
    – Newalp
    Mar 16, 2019 at 21:24
17
  1. install readline-devel package.
  2. recompile python with readline module
  3. Bingo!
2
  • 2
    Thanks for improving my quality of life! {;-) On Centos 5.5, that's yum install readline-devel and you don't have to explicitly specify readline in the recompilation
    – michela
    Oct 26, 2010 at 1:34
  • 3
    How do we recompile Python? Do I need to install extract python from source (./configure, make, make install), set up virtualenv and install all my packages again? May 6, 2013 at 19:18
12

Here are the steps which worked for me in ubuntu 12.04 for python 3.3.

1) open teminal and write sudo apt-get install libreadline-dev

2) download the source file of python 3.3.2 from http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz

3) extract it and navigate to the Python-3.3.2/ directory in a shell

4) execute the following command:

./configure
make
make test
sudo make install
7

Was impacted after upgrading Mac to High Sierra, this successfully resolved it for me:

brew unlink python
xcode-select --install
brew install python
6

On CentOS, I fix this by

yum install readline-devel

and then recompile python 3.4.

On OpenSUSE, I fix this by

pip3 install readline

following Valerio Crini's answer.

Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.

1
  • Good call with the readline-devel tip. It worked for me on a Centos6.5 machine rumming python 2.7.8. I directly called pip install readline without recompiling python and it worked fine.
    – Arjun
    Jul 13, 2015 at 21:09
6

readline module has been deprecated which will cause invalid pointer error in latest python versions when executing quit() or exit() in python shell. pip install gnureadline instead

1
  • I'm not sure why this answer isn't upvoted higher. Seems to be an instant fix for me. Jul 15, 2023 at 7:06
5

If you use Anaconda Python, you can fix this by running:

conda install readline

Worked for me!

2
  • 3
    In my case, readline is already installed, I have to do conda install ncurses to make it work.
    – whenov
    Feb 19, 2017 at 5:57
  • This worked for me, but only after I uninstalled and then reinstalled python
    – Lahav
    Mar 8, 2021 at 0:14
4

I fixed this by doing the following:

  • yum install readline-devel
  • pip install readline

    • I encountered another error here:

      gcc: readline/libreadline.a: No such file or directory

      gcc: readline/libhistory.a: No such file or directory

      I fixed this by installing patch:

      yum install patch

After that I managed to run pip install readline successfully which solved the escape characters in my python shell.

FYI, I'm using RedHat

1
  • CentOS 6.8, Python2.7.12, a success
    – kkzxak47
    Nov 21, 2016 at 6:34
4

None of these answers worked for me on two different version of Ubuntu. What worked for me, but isn't a true fix, is wrapping my python code in a call to rlwrap (available in the ubuntu repositories):

rlwrap python mycode.py

3

For those using conda, installing the readline package from conda-forge channel will fix the problem:

conda install -c conda-forge readline=6.2
2

Did you call ssh with the -t parameter to tell ssh to allocate a virtual terminal for you?

From the man page:

-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Additionally you may also have to set the TERM environment variable on the server correctly as suggested in another post.

0
2

On Mac OS X Mojave 10.14.6 with various historical installs via brew I solved this with:

brew reinstall python2

There is likely no magic bullet given everyone has a different install scenario. I tried the above as well so it may have been a combination of a few of the answers. Brew defaults to python3 so if you installed the python2 package it also needs to be reinstalled.

2

In Unbuntu or Mint, if you are using pyenv,

sudo apt install libreadline-dev
pyenv uninstall 3.8.8
pyenv install 3.8.8

Once installing libreadline-dev, you don't need to install pip install gnureadline on every python version.

1

Have you tried using a different SSH client? Some SSH clients have special, built-in keymappings for different remote processes. I ran into this one a lot with emacs.

What client are you using? I'd recommend trying Putty and SecureCRT to compare their behavior.

5
  • Hmm, I just use the ssh command in the terminal (ssh -v says OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003).
    – Frank
    May 21, 2009 at 14:01
  • Try out Putty (its free) and get back to us :)
    – JoshJordan
    May 21, 2009 at 14:10
  • Okay, I just installed putty and used it to connect to the machine, but the behavior there is the same.
    – Frank
    May 21, 2009 at 14:20
  • :( Definitely a server-side issue then. I will look into it and get back to you.
    – JoshJordan
    May 21, 2009 at 14:21
  • Using the ssh command to connect to some other machine works fine: There, I have no problems with the arrow keys. It seems to be the one particular system that has trouble.
    – Frank
    May 21, 2009 at 14:23
0

How's your env variable $TERM set [a] when things work fine and [b] when they don't? Env settings are often the key to such problems.

1
  • 1
    I just tried some TERM variable values: vt102, vt220, ansi, xterm, but none of them changed the behavior.
    – Frank
    May 21, 2009 at 14:06
0

Try getting a key code library running on the server. If that does not work try to download a library with read-key ability.

0

I was trying build Python 2.7 on Ubuntu 14.0. You will need libreadline-dev. However, if you get it from apt-get, the current version is 6.3, which is incompatible with Python 2.7 (not sure about Python 3). For example, the data type "Function" and "CPPFunction", which were defined in previous versions of readline has been removed in 6.3, as reported here:

https://github.com/yyuu/pyenv/issues/126

That is to say you need to get the source code of an earlier version of readline. I installed libreadline 5.2 from apt-get for the library, and get the source code of 5.2 for the header files. Put them in /usr/include.

Finally the issue has been resolved.

0

On MacOsx, I fixed this by reinstalling readline

brew reinstall readline
0

you can switch from 'sh" to "bash" by

$ /sh/bash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.