在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
如果您使用 Anaconda Python,您可以通过运行以下命令来修复此问题:
Worked for me!
If you use Anaconda Python, you can fix this by running:
Worked for me!
我通过执行以下操作修复了此问题:
pip install readline
我在这里遇到了另一个错误:
gcc: readline/libreadline.a: 没有这样的文件或目录
gcc: readline/libhistory.a: 没有这样的文件或目录
我通过安装
补丁
修复了这个问题:yum 安装补丁
之后我设法运行
pip install readline< /code> 成功解决了我的 python shell 中的转义字符。
仅供参考,我正在使用 RedHat
I fixed this by doing the following:
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
在两个不同版本的 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
对于使用 conda 的用户,从 conda-forge 频道安装 readline 包将解决问题:
For those using conda, installing the readline package from conda-forge channel will fix the problem:
在 Unbuntu 或 Mint,
如果您使用的是pyenv,
一旦安装了libreadline-dev,您不需要在每个python版本上安装pip install gnureadline。
In Unbuntu or Mint,
if you are using pyenv,
Once installing libreadline-dev, you don't need to install pip install gnureadline on every python version.
您是否使用 -t 参数调用 ssh 来告诉 ssh 为您分配虚拟终端?
从手册页:
此外,您可能还必须按照另一篇文章中的建议在服务器上正确设置TERM环境变量。
Did you call ssh with the -t parameter to tell ssh to allocate a virtual terminal for you?
From the man page:
Additionally you may also have to set the TERM environment variable on the server correctly as suggested in another post.
在 Mac OS X Mojave 10.14.6 上,通过
brew
进行了各种历史安装,我解决了这个问题:鉴于每个人都有不同的安装场景,可能没有灵丹妙药。 我也尝试了上述内容,因此它可能是一些答案的组合。 Brew 默认使用
python3
,因此如果您安装了python2
软件包,也需要重新安装。On Mac OS X Mojave 10.14.6 with various historical installs via
brew
I solved this with: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 thepython2
package it also needs to be reinstalled.您是否尝试过使用不同的 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.
在 MacOsx 上,我通过重新安装 readline 解决了这个问题
On MacOsx, I fixed this by reinstalling readline
当一切正常时,你的环境变量 $TERM 设置如何[a],当一切正常时如何设置 [b]? 环境设置往往是此类问题的关键。
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.
尝试让一个关键代码库在服务器上运行。 如果这不起作用,请尝试下载具有读取密钥功能的库。
Try getting a key code library running on the server. If that does not work try to download a library with read-key ability.
我正在尝试在 Ubuntu 14.0 上构建 Python 2.7。 您将需要 libreadline-dev。 但是,如果你从 apt-get 获取它,当前版本是 6.3,它与 Python 2.7 不兼容(不确定 Python 3)。 例如,在 readline 的早期版本中定义的数据类型“Function”和“CPPFunction”已在 6.3 中删除,如下所示:
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.
您可以通过以下方式从“sh”切换到“bash”
you can switch from 'sh" to "bash" by
我通过安装 readline 包解决了这个问题:
I've solved this issue by installing
readline
package:在 OS X 上,我有不同的问题。
当我使用系统python shell时,按键没有问题,但在virtualenv中出现问题。 我尝试重新安装/升级 virtualenv/readline 但没有解决任何问题。
当我尝试在有问题的 python shell 中导入 readline 时,收到此错误消息:
原因有
/usr/local/opt/readline/lib/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:Cause there is
/usr/local/opt/readline/lib/libreadline.7.dylib
but notlibreadline.6.dylib
, so I make a symbol link:Problem has been solved!
在 OS X 上,Xcode 更新有时会破坏
readline
。 解决方案:如果问题仍然存在,请尝试使用
pip
删除readline
并使用easy_install
安装它:On OS X, Xcode updates sometimes break
readline
. Solution:If the problem still persists, try to remove
readline
usingpip
and install it usingeasy_install
:在 OS X 上,使用 python 3.5 和 virtualenv
在解释器中执行以下操作:
现在箭头键应该可以正常工作。
其他信息...
请注意,截至 2015 年 10 月 1 日 - readline 已被弃用(来源 https://github.com/ludwigschwardt/python-readline)
使用 gnureadline 代替(请参阅:https://github.com/ludwigschwardt/python-gnureadline)
如果我使用 python 3.5 安装 readline 而不是 gnureadline,则在尝试导入解释器后会收到错误:
On OS X, using python 3.5 and virtualenv
In the interpreter do:
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:
看起来 readline 未启用。 检查是否定义了 PYTHONSTARTUP 变量,对我来说它指向 /etc/pythonstart 并且该文件在交互之前由 python 进程执行,这设置了 readline/history 处理。
感谢@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
我最近遇到了这个问题,在阅读了很多关于 pip install readline (不适用于 mac osx)和 pip install gnureadline 并不满意之后,现在是我的设置允许在任何 python 控制台中使用箭头键:
pip install gnureadline
安装 gnureadline现在您可以执行
import gnureadline
并且箭头键应该按预期工作。 要使它们自动工作,请按照以下步骤操作:~/.startup.py
:import gnureadline
~/.bash_profile
:export PYTHONSTARTUP=~/.startup.py
有一件事不起作用,但在我之前的设置中做了:在
pdb.set_trace()< 上自动导入 gnureadline /代码>。 如果有人对这个问题有好的解决方案,我将不胜感激。
I have run into this issue recently and after reading a lot about
pip install readline
(does not work for mac osx) andpip install gnureadline
and not being satisfied, this is now my setup which enables using arrow keys in any python console: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:~/.startup.py
:import gnureadline
~/.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.我在 Ubuntu 16.04 LTS 上遇到了 Python 3.6.x 的 shell 历史记录(选项卡/箭头命令)问题。
Python 3.6.x 是从源代码安装的。
对我来说解决的是使用此命令行安装模块“gnureadline”,如 user12345 所说:
:)
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:
:)
以下是在 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< /a>
3) 解压它并在 shell 中导航到 Python-3.3.2/ 目录
4) 执行以下命令:
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:
将 Mac 升级到 High Sierra 后受到影响,这成功解决了我的问题:
Was impacted after upgrading Mac to High Sierra, this successfully resolved it for me:
readline 模块已被弃用,这将在最新的 python 版本中在 python shell 中执行 quit() 或 exit() 时导致无效指针错误。
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在 CentOS 上,我通过
yum install readline-devel
修复此问题,然后重新编译 python 3.4。
在 OpenSUSE 上,我按照
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
following Valerio Crini's answer.
Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.