Seeing escape characters when pressing the arrow keys in python shell
Asked Answered
W

25

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.

Whittaker answered 21/5, 2009 at 13:55 Comment(5)
I think this belongs on server-fault. This is caused by an incorrect terminal type.Caridadcarie
I agree with cartman below that it's a readline issue, not a terminal type issue.Whittaker
easy_install readline and then easy_install ipython works perfect in mac.Flittermouse
install anaconda2 or anaconda3 and set .pystartup please see linkTorre
@LeonWANG gives a bad answer - don't install something as huge as anaconda just to get your .pystartup file working. Completely unnecessary.Lymphosarcoma
C
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

Clari answered 21/5, 2009 at 14:27 Comment(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.Whittaker
After some googling it seems python on that system might have to be recompiled, after installing readline-devel.Whittaker
Yes you will have to recompile.Clari
The environment variable is PYTHONSTARTUP, not PYTHONSTART. Not sure about the distribution-specific /etc/pythonstart file.Blackmarket
Would you mind showing the script contents of your /etc/pythonstart file so we can see how it sets up readline and history?Madonna
Actually, reading docs.python.org/2/tutorial/interactive.html gives all the info needed for a pystartup file.Madonna
On OSX with brew, all I had to do was brew reinstall python3Tallith
P
137

I've solved this issue by installing readline package:

pip install readline
Pashm answered 14/10, 2014 at 8:46 Comment(16)
I had to install libncurses-dev on my Ubuntu machine, then readline installed correctly.Ammonic
Installed ncurses-devel on CentOS and then readline installed no problem. Interactive shell is now working.Thighbone
yum install ncurses-develRogozen
I had this problem after updating conda, running conda install readline solved the issue.Norvell
can't pip install readline on my OS X machine, fails every time even after successfully doing brew install readlineGrooms
I had to install gnureadline instead, see https://mcmap.net/q/119383/-python-3-6-1-crashed-after-readline-module-installed/2846923Wop
it did fixed my problem in centos, python 2.7.6Chrisman
@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!Raina
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.Guttate
@sh37211: ´conda install readline´ works if you are using conda to create python environment. This works for me of Mac for conda environment.Lawhorn
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) Deaconess
On CentOS, you might also need sudo yum install patch before installing readline will work.Thunderstone
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.Selfexecuting
plus import readlineBartel
error: this module is not meant to work on WindowsImplement
Thank you, so much! I've just put up with this beating for such a long time! So glad to finally have some closure.Home
A
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!

Anna answered 10/1, 2017 at 2:50 Comment(10)
After looking and looking I was able to fix the issue with this suggestion. Thanks!Pedestrianism
Won't this cause a problem if something calls v6, not realising that it's actually v7?Alamo
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.Alamo
@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.Ditch
+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!Projectile
you are the best.Blandina
Thank you. This is the only solution that worked for me.Hover
Thanks for this, nice option to just deal with this once.Depalma
You should have 'ln -s ...' in there for a symbolic link, otherwise it's a hard link.Leontina
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.Beslobber
O
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
Oleta answered 25/10, 2016 at 1:20 Comment(7)
is there an equivalent if you are using system Python 2.7.10?Grooms
@Grooms I've updated the answer with an easy_install optionOleta
OSX 10.12 users also: #7376045Serieswound
For python 3.4 "easy_install-3.4 readline" solved the problem.Bracteate
Using easy_install works for me, while the one with pip doesn't work.Gilemette
This combined with the brew link readline (even though it was already linked) fixed it for me.Independent
This approach worked for me also to fix a version inconsistency between the Python readline package (wanting v7 of the underlying OS-level readline) and the OS-level readline package (providing only v8) on MacOS.Bouillon
V
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
Veedis answered 26/5, 2017 at 21:4 Comment(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?Hover
@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 automaticallyVeedis
This is the most up-to-date answer for this question.Trotyl
C
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

Clari answered 21/5, 2009 at 14:27 Comment(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.Whittaker
After some googling it seems python on that system might have to be recompiled, after installing readline-devel.Whittaker
Yes you will have to recompile.Clari
The environment variable is PYTHONSTARTUP, not PYTHONSTART. Not sure about the distribution-specific /etc/pythonstart file.Blackmarket
Would you mind showing the script contents of your /etc/pythonstart file so we can see how it sets up readline and history?Madonna
Actually, reading docs.python.org/2/tutorial/interactive.html gives all the info needed for a pystartup file.Madonna
On OSX with brew, all I had to do was brew reinstall python3Tallith
E
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.

Echoechoic answered 11/3, 2019 at 13:30 Comment(0)
H
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

:)

Heidyheifer answered 21/5, 2018 at 18:24 Comment(3)
I tried all other solutions but this one worked for me for my Python 3.6Gainor
make sure to call import gnureadline in the shell for it to workEchoechoic
No need to use sudoPartida
D
17
  1. install readline-devel package.
  2. recompile python with readline module
  3. Bingo!
Duplet answered 10/5, 2010 at 20:4 Comment(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 recompilationHornbeam
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?Wrennie
F
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
Flagpole answered 30/6, 2013 at 19:44 Comment(0)
L
7

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

brew unlink python
xcode-select --install
brew install python
Lanner answered 10/10, 2017 at 0:13 Comment(0)
A
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.

Atomicity answered 24/1, 2015 at 22:32 Comment(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.Wrongdoer
M
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

Malmsey answered 28/11, 2018 at 2:56 Comment(1)
I'm not sure why this answer isn't upvoted higher. Seems to be an instant fix for me.Cardiomegaly
P
5

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

conda install readline

Worked for me!

Picrate answered 10/2, 2017 at 21:32 Comment(2)
In my case, readline is already installed, I have to do conda install ncurses to make it work.Knowles
This worked for me, but only after I uninstalled and then reinstalled pythonPamphleteer
C
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

Connection answered 15/1, 2016 at 3:24 Comment(1)
CentOS 6.8, Python2.7.12, a successParfleche
A
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

Allamerican answered 16/10, 2019 at 15:31 Comment(0)
S
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
Syllabus answered 20/9, 2017 at 8:19 Comment(0)
I
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.

Injection answered 21/5, 2009 at 14:22 Comment(0)
A
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.

Australian answered 11/8, 2019 at 1:53 Comment(0)
A
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.

Arjan answered 22/3, 2021 at 3:55 Comment(0)
P
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.

Plott answered 21/5, 2009 at 13:57 Comment(5)
Hmm, I just use the ssh command in the terminal (ssh -v says OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003).Whittaker
Try out Putty (its free) and get back to us :)Plott
Okay, I just installed putty and used it to connect to the machine, but the behavior there is the same.Whittaker
:( Definitely a server-side issue then. I will look into it and get back to you.Plott
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.Whittaker
P
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.

Petronille answered 21/5, 2009 at 14:3 Comment(1)
I just tried some TERM variable values: vt102, vt220, ansi, xterm, but none of them changed the behavior.Whittaker
A
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.

Arrears answered 25/6, 2013 at 20:49 Comment(0)
T
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.

Toadeater answered 13/5, 2015 at 16:14 Comment(0)
D
0

On MacOsx, I fixed this by reinstalling readline

brew reinstall readline
Dorser answered 27/9, 2019 at 18:59 Comment(0)
M
0

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

$ /sh/bash
Monastic answered 9/2, 2022 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.