标签归档:arrow-keys

在python shell中按箭头键时看到转义字符

问题:在python shell中按箭头键时看到转义字符

在交互式python shell之类的shell中,通常可以使用箭头键在当前行中移动或获取先前的命令(使用向上箭头)等。

但是,当我将SSH切换到另一台机器并从python那里启动时,我得到如下会话:

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

最后符来自上箭头。或者,使用左箭头:

>>> impor^[[D

我怎样才能解决这个问题?

在常规bash中,箭头键可以正常工作。奇怪的行为只是在交互式python(或perl等)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.


回答 0

似乎未启用readline。检查PYTHONSTARTUP变量是否已定义,对我而言,它指向/etc/pythonstart该文件,并且文件在交互之前由python进程执行,从而设置了读取行/历史记录处理。

感谢@chown,这里是有关此文档:http ://docs.python.org/2/tutorial/interactive.html

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


回答 1

我已经通过安装readline软件包解决了这个问题:

pip install readline

I’ve solved this issue by installing readline package:

pip install readline

回答 2

在OS X上,我有不同的问题。

当我使用系统python shell时,键没有问题,但是virtualenv中有问题。我会尝试重新安装/升级virtualenv / readline,但未解决任何问题。

当我尝试使用import readline有问题的python shell时,收到以下错误消息:

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

因为有/usr/local/opt/readline/lib/libreadline.7.dylib但没有libreadline.6.dylib,所以我做了一个符号链接:

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

问题已解决!

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!


回答 3

在OS X上,Xcode更新有时会中断readline。解:

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

如果问题仍然存在,请尝试删除并readline使用pip安装它easy_install

pip3 uninstall readline
easy_install readline

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

回答 4

在OS X上,使用python 3.5和virtualenv

$ pip install gnureadline

在解释器中执行以下操作:

import gnureadline

现在,箭头键应该可以正常工作。


附加信息…

请注意,自2015年10月1日起-readline已被弃用(来源https://github.com/ludwigschwardt/python-readline

改用gnureadline(请参阅:https : //github.com/ludwigschwardt/python-gnureadline

如果我使用python 3.5安装readline而不是gnureadline,则尝试导入解释器后收到错误消息:

>>> 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

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

回答 5

我最近遇到过这个问题,在阅读了很多有关pip install readline(不适用于mac osx)并且pip install gnureadline不满意之后,现在这是我的设置,该设置允许在任何python控制台中使用箭头键:

  1. 使用安装gnureadline pip install gnureadline

现在您可以执行操作,import gnureadline并且箭头键应该可以按预期工作。要使它们自动工作,请执行以下步骤:

  1. 创建(或附加到)文件~/.startup.pyimport gnureadline
  2. 追加到文件~/.bash_profileexport PYTHONSTARTUP=~/.startup.py

在我之前的设置中没有起作用的一件事是:在上自动导入gnureadline pdb.set_trace()。如果有人对这个问题有一个好的解决方案,我将不胜感激。

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.


回答 6

  1. 安装readline-devel软件包。
  2. 用readline模块重新编译python
  3. 答对了!
  1. install readline-devel package.
  2. recompile python with readline module
  3. Bingo!

回答 7

我在Ubuntu 16.04 LTS上遇到Python 3.6.x的外壳历史记录(选项卡/箭头命令)问题。

Python 3.6.x是从源代码安装的。

对我来说解决的是使用以下命令行安装user12345所说的模块“ gnureadline”:

sudo pip3.6 install gnureadline

:)

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

:)


回答 8

这是在ubuntu 12.04 for python 3.3中为我工作的步骤。

1)打开终端并编写 sudo apt-get install libreadline-dev

2)从http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz下载python 3.3.2的源文件

3)解压缩并导航到Shell中的Python-3.3.2 /目录

4)执行以下命令:

./configure
make
make test
sudo make install

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

回答 9

将Mac升级到High Sierra后受到影响,这为我成功解决了它:

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

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

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

回答 10

在CentOS上,我通过

yum install readline-devel

然后重新编译python 3.4。

在OpenSUSE上,我通过

pip3 install readline

按照Valerio Crini的回答。

也许“ pip3 install readline”是一个通用解决方案。尚未在我的CentOS上尝试过。

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.


回答 11

我通过以下操作解决了这个问题:

  • 百胜安装readline-devel
  • 点安装readline

    • 我在这里遇到另一个错误:

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

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

      我通过安装解决了这个问题patch

      yum install patch

之后,我成功运行pip install readline,解决了我的python shell中的转义字符。

仅供参考,我正在使用RedHat

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


回答 12

如果使用Anaconda Python,则可以通过运行以下命令解决此问题:

conda install readline

为我工作!

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

conda install readline

Worked for me!


回答 13

对于使用conda的用户,从conda-forge频道安装readline软件包将解决此问题:

conda install -c conda-forge readline=6.2

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

conda install -c conda-forge readline=6.2

回答 14

您是否使用-t参数调用ssh 来告诉ssh为您分配虚拟终端?

从手册页:

-t
强制伪tty分配。这可用于在远程计算机上执行任意基于屏幕的程序,这可能非常有用,例如在实现菜单服务时。即使ssh没有本地tty,多个-t选项也会强制tty分配。

另外,您可能还必须按照另一篇文章中的建议,在服务器上正确设置TERM环境变量。

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.


回答 15

在Mac OS X Mojave 10.14.6上,通过各种历史安装,brew我使用以下方法解决了此问题:

brew reinstall python2

鉴于每个人都有不同的安装方案,因此可能没有万灵药。我也尝试了上述方法,因此可能是一些答案的组合。Brew默认为默认值,python3因此,如果您安装了python2软件包,则还需要重新安装它。

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.


回答 16

这些答案都不适用于两个不同版本的Ubuntu。什么对我有用,但不是真正的解决办法,是将我的python代码包装在调用中rlwrap(可在ubuntu存储库中找到):

rlwrap python mycode.py

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


回答 17

您是否尝试过使用其他SSH客户端?某些SSH客户端具有用于不同远程进程的特殊内置键映射。我经常使用emacs遇到这一问题。

您正在使用什么客户端?我建议尝试使用Putty和SecureCRT来比较它们的行为。

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.


回答 18

readline模块已被弃用,这将在python shell中执行quit()或exit()时在最新的python版本中导致无效的指针错误。 pip install gnureadline代替

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


回答 19

当一切正常时,您的环境变量$ TERM如何设置?环境设置通常是解决此类问题的关键。

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.


回答 20

尝试让密钥代码库在服务器上运行。如果这样不起作用,请尝试下载具有读键功能的库。

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


回答 21

我试图在Ubuntu 14.0上构建Python 2.7。您将需要libreadline-dev。但是,如果从apt-get获取它,则当前版本为6.3,该版本与Python 2.7不兼容(不确定Python 3)。例如,在6.3版本中已删除了在readline的早期版本中定义的数据类型“ Function”和“ CPPFunction”,如下所示:

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

也就是说,您需要获取早期版本的readline的源代码。我从apt-get中为该库安装了libreadline 5.2,并获得了头文件5.2的源代码。将它们放在/ usr / include中。

终于问题解决了。

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.


回答 22

在MacOsx上,我通过重新安装readline修复了此问题

brew reinstall readline

On MacOsx, I fixed this by reinstalling readline

brew reinstall readline