235

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.

Braiam
  • 1
  • 11
  • 47
  • 78
Frank
  • 64,140
  • 93
  • 237
  • 324
  • I think this belongs on server-fault. This is caused by an incorrect terminal type. – epochwolf May 21 '09 at 14:56
  • I agree with cartman below that it's a readline issue, not a terminal type issue. – Frank May 21 '09 at 15:10
  • 2
    easy_install readline and then easy_install ipython works perfect in mac. – gnuyoga Oct 12 '13 at 07:52
  • install anaconda2 or anaconda3 and set .pystartup please see [link](https://stackoverflow.com/questions/48134649/how-to-set-configuration-for-readline-in-python?noredirect=1#comment83246563_48134649) – Leon WANG Jan 07 '18 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 '18 at 15:44

25 Answers25

134

I've solved this issue by installing readline package:

pip install readline
Aleksandr Kovalev
  • 3,508
  • 4
  • 34
  • 36
Valerio Crini
  • 1,829
  • 3
  • 13
  • 14
  • 32
    I had to install `libncurses-dev` on my Ubuntu machine, then readline installed correctly. – Amir Eldor Dec 22 '15 at 19:23
  • 8
    Installed `ncurses-devel` on CentOS and then `readline` installed no problem. Interactive shell is now working. – one.time Apr 07 '16 at 15:26
  • 2
    yum install ncurses-devel – Alex Punnen Jul 06 '16 at 06:21
  • I had this problem after updating conda, running `conda install readline` solved the issue. – emre Nov 08 '16 at 09:59
  • 4
    can't `pip install readline` on my OS X machine, fails every time even after successfully doing `brew install readline` – user5359531 Dec 12 '16 at 04:03
  • 27
    I had to install `gnureadline` instead, see https://stackoverflow.com/q/43013060/2846923 – The Guy with The Hat Nov 05 '18 at 21:37
  • it did fixed my problem in centos, python 2.7.6 – Gary Gauh Jan 04 '19 at 05:12
  • 5
    @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 '19 at 08:49
  • `conda install readline` had no effect on my (updated) anaconda Ubuntu system. `conda install gnureadline` turned up no package. But `pip install gnureadline` fixed my problem. – sh37211 Feb 24 '19 at 19:21
  • 1
    @sh37211: ´conda install readline´ works if you are using conda to create python environment. This works for me of Mac for conda environment. – Good Will Mar 08 '19 at 13:33
  • I'm getting invalid pointer error when importing any package. See below `import pygame free(): invalid pointer Aborted (core dumped)` `import os free(): invalid pointer Aborted (core dumped) ` – vishless Apr 18 '20 at 05:21
  • On CentOS, you might also need `sudo yum install patch` before installing readline will work. – Elad Avron Nov 17 '20 at 12:26
  • if on windows, install `pyreadline` honestly shocked, but it totally worked. was running cygwin and i had the problem in a venv, though no escape characters were printed. – starwarswii Oct 15 '21 at 20:29
  • plus `import readline` – M. Gruber Feb 03 '22 at 13:00
  • 1
    error: this module is not meant to work on Windows – Erik Aronesty Mar 02 '22 at 20:43
85

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!

raptor.zh
  • 1,020
  • 7
  • 7
  • After looking and looking I was able to fix the issue with this suggestion. Thanks! – gl051 Jan 18 '17 at 00:50
  • 1
    Won't this cause a problem if something calls v6, not realising that it's actually v7? – Adam Barnes Feb 24 '17 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. – Adam Barnes Feb 24 '17 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 '17 at 18:22
  • +100 with system python 2.7.11 I had the same problem. python 3.6.4 (installed using pyenv was fine, but we are using system python for CI servers). Importing gnureadline helped, but creating this link is easier / less changes - thanks! – Stan Kurdziel Apr 05 '18 at 00:06
  • you are the best. – An Se Apr 27 '18 at 18:32
  • Thank you. This is the only solution that worked for me. – Anton Codes Mar 28 '19 at 14:50
  • Thanks for this, nice option to just deal with this once. – Chris Barry May 02 '19 at 17:47
  • You should have 'ln -s ...' in there for a symbolic link, otherwise it's a hard link. – racitup May 19 '19 at 19:17
  • 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 '19 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
Max Malysh
  • 29,384
  • 19
  • 111
  • 115
42

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
user12345
  • 2,876
  • 2
  • 24
  • 25
  • 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? – Anton Codes Mar 28 '19 at 14:55
  • 1
    @Panchishin try to use the [PYTHONSTARTUP](https://docs.python.org/3/using/cmdline.html#envvar-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](https://stackoverflow.com/questions/9409762/run-python-command-line-interpreter-with-imports-loaded-automatically) – user12345 Apr 01 '19 at 23:58
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

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
ismail
  • 46,010
  • 9
  • 86
  • 95
  • 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 '09 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 '09 at 14:49
  • Yes you will have to recompile. – ismail May 22 '09 at 10:06
  • 1
    The environment variable is `PYTHONSTARTUP`, not `PYTHONSTART`. Not sure about the distribution-specific `/etc/pythonstart` file. – Ned Deily Dec 16 '11 at 18:04
  • Would you mind showing the script contents of your `/etc/pythonstart` file so we can see how it sets up readline and history? – chown Jan 21 '13 at 18:54
  • 1
    Actually, reading http://docs.python.org/2/tutorial/interactive.html gives all the info needed for a pystartup file. – chown Jan 21 '13 at 19:08
  • 7
    On OSX with brew, all I had to do was `brew reinstall python3` – Ranhiru Jude Cooray Sep 16 '17 at 04:01
27

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.

David Schumann
  • 13,380
  • 9
  • 75
  • 96
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

:)

Serza
  • 321
  • 2
  • 4
16
  1. install readline-devel package.
  2. recompile python with readline module
  3. Bingo!
Eric Wang
  • 301
  • 3
  • 6
  • 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 '10 at 01: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? – Muhammad Waqar May 06 '13 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
meetar
  • 7,443
  • 8
  • 42
  • 73
aabhas
  • 416
  • 5
  • 9
7

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

brew unlink python
xcode-select --install
brew install python
Big Perm
  • 81
  • 1
  • 4
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.

peter pan gz
  • 331
  • 3
  • 5
  • 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 '15 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

Bill Greens
  • 73
  • 2
  • 4
5

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

conda install readline

Worked for me!

Adam Stewart
  • 2,001
  • 1
  • 16
  • 16
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

DaVinci
  • 1,391
  • 2
  • 12
  • 27
3

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

juanpaolo
  • 625
  • 5
  • 14
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
Tomas Ruiz
  • 139
  • 4
  • 15
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.

rjmoggach
  • 1,458
  • 15
  • 27
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.

lothar
  • 19,853
  • 5
  • 45
  • 59
1

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.

won0
  • 11
  • 2
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.

JoshJordan
  • 12,676
  • 10
  • 53
  • 63
  • 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 '09 at 14:01
  • Try out Putty (its free) and get back to us :) – JoshJordan May 21 '09 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 '09 at 14:20
  • :( Definitely a server-side issue then. I will look into it and get back to you. – JoshJordan May 21 '09 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 '09 at 14:23
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.

Niall C.
  • 10,878
  • 7
  • 69
  • 61
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.

AeroD
  • 53
  • 7
0

On MacOsx, I fixed this by reinstalling readline

brew reinstall readline
0

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

$ /sh/bash
Duc Toan Pham
  • 474
  • 6
  • 6
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.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 1
    I just tried some TERM variable values: vt102, vt220, ansi, xterm, but none of them changed the behavior. – Frank May 21 '09 at 14:06