ChatGPT解决这个技术问题 Extra ChatGPT

Seeing escape characters when pressing the arrow keys in python shell

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.

I think this belongs on server-fault. This is caused by an incorrect terminal type.
I agree with cartman below that it's a readline issue, not a terminal type issue.
easy_install readline and then easy_install ipython works perfect in mac.
install anaconda2 or anaconda3 and set .pystartup please see link
@LeonWANG gives a bad answer - don't install something as huge as anaconda just to get your .pystartup file working. Completely unnecessary.

A
Aleksandr Kovalev

I've solved this issue by installing readline package:

pip install readline

I had to install libncurses-dev on my Ubuntu machine, then readline installed correctly.
Installed ncurses-devel on CentOS and then readline installed no problem. Interactive shell is now working.
can't pip install readline on my OS X machine, fails every time even after successfully doing brew install readline
I had to install gnureadline instead, see stackoverflow.com/q/43013060/2846923
@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!
r
raptor.zh

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!


After looking and looking I was able to fix the issue with this suggestion. Thanks!
Won't this cause a problem if something calls v6, not realising that it's actually v7?
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.
@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.
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.
M
Max Malysh

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

is there an equivalent if you are using system Python 2.7.10?
@user5359531 I've updated the answer with an easy_install option
Using easy_install works for me, while the one with pip doesn't work.
This combined with the brew link readline (even though it was already linked) fixed it for me.
Q
Qix - MONICA WAS MISTREATED

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


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.
After some googling it seems python on that system might have to be recompiled, after installing readline-devel.
The environment variable is PYTHONSTARTUP, not PYTHONSTART. Not sure about the distribution-specific /etc/pythonstart file.
Actually, reading docs.python.org/2/tutorial/interactive.html gives all the info needed for a pystartup file.
On OSX with brew, all I had to do was brew reinstall python3
u
user12345

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

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?
@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
D
David Schumann

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:

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:

create (or append to) file ~/.startup.py: import gnureadline 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.


S
Serza

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

:)


I tried all other solutions but this one worked for me for my Python 3.6
make sure to call import gnureadline in the shell for it to work
No need to use sudo
E
Eric Wang

install readline-devel package. recompile python with readline module Bingo!


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
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?
m
meetar

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

B
Big Perm

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

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

p
peter pan gz

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.


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.
A
Adam Stewart

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

conda install readline

Worked for me!


In my case, readline is already installed, I have to do conda install ncurses to make it work.
This worked for me, but only after I uninstalled and then reinstalled python
D
DaVinci

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


j
juanpaolo

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

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


CentOS 6.8, Python2.7.12, a success
T
Tomas Ruiz

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

conda install -c conda-forge readline=6.2

l
lothar

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.


B
Bill Greens

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


r
rjmoggach

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.


J
JoshJordan

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.


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

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.


I just tried some TERM variable values: vt102, vt220, ansi, xterm, but none of them changed the behavior.
N
Niall C.

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


A
AeroD

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.


S
Shyam Shinde

On MacOsx, I fixed this by reinstalling readline

brew reinstall readline

w
won0

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.


D
Duc Toan Pham

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

$ /sh/bash