Seeing escape characters when pressing the arrow k

2019-01-04 19:43发布

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.

19条回答
forever°为你锁心
2楼-- · 2019-01-04 19:55

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
查看更多
Lonely孤独者°
3楼-- · 2019-01-04 19:55

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

:)

查看更多
何必那么认真
4楼-- · 2019-01-04 19:56

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

conda install -c conda-forge readline=6.2
查看更多
爷的心禁止访问
5楼-- · 2019-01-04 19:57

I've solved this issue by installing readline package:

pip install readline
查看更多
疯言疯语
6楼-- · 2019-01-04 19:57

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

conda install readline

Worked for me!

查看更多
可以哭但决不认输i
7楼-- · 2019-01-04 19:59

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
查看更多
登录 后发表回答