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

发布于 2024-07-20 04:06:49 字数 375 浏览 10 评论 0原文

在像交互式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(25

东京女 2024-07-27 04:06:50

如果您使用 Anaconda Python,您可以通过运行以下命令来修复此问题:

conda install readline

Worked for me!

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

conda install readline

Worked for me!

太傻旳人生 2024-07-27 04:06:50

我通过执行以下操作修复了此问题:

  • yum install readline-devel
  • 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:

  • 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

只是偏爱你 2024-07-27 04:06:50

在两个不同版本的 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

淡墨 2024-07-27 04:06:50

对于使用 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
×眷恋的温暖 2024-07-27 04:06:50

在 Unbuntu 或 Mint,
如果您使用的是pyenv

sudo apt install libreadline-dev
pyenv uninstall 3.8.8
pyenv install 3.8.8

一旦安装了libreadline-dev,您不需要在每个python版本上安装pip install gnureadline。

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.

橘香 2024-07-27 04:06:50

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

从手册页:

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

小梨窩很甜 2024-07-27 04:06:50

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

浪推晚风 2024-07-27 04:06:50

您是否尝试过使用不同的 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.

离不开的别离 2024-07-27 04:06:50

在 MacOsx 上,我通过重新安装 readline 解决了这个问题

brew reinstall readline

On MacOsx, I fixed this by reinstalling readline

brew reinstall readline
九八野马 2024-07-27 04:06:50

当一切正常时,你的环境变量 $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.

浅听莫相离 2024-07-27 04:06:50

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

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

末蓝 2024-07-27 04:06:50

我正在尝试在 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.

最偏执的依靠 2024-07-27 04:06:50

您可以通过以下方式从“sh”切换到“bash”

$ /sh/bash

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

$ /sh/bash
毁梦 2024-07-27 04:06:49

我通过安装 readline 包解决了这个问题:

pip install readline

I've solved this issue by installing readline package:

pip install readline
清晨说晚安 2024-07-27 04:06:49

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

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

当我尝试在有问题的 python shell 中导入 readline 时,收到此错误消息:

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!

Bonjour°[大白 2024-07-27 04:06:49

在 OS X 上,Xcode 更新有时会破坏 readline。 解决方案:

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

如果问题仍然存在,请尝试使用 pip 删除 readline 并使用 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
惜醉颜 2024-07-27 04:06:49

在 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
浅沫记忆 2024-07-27 04:06:49

看起来 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

伊面 2024-07-27 04:06:49

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

  1. 使用 pip install gnureadline 安装 gnureadline

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

  1. 创建(或附加到)文件~/.startup.pyimport gnureadline
  2. 附加到文件~/.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) 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.

说好的呢 2024-07-27 04:06:49

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

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

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

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

:)

帅的被狗咬 2024-07-27 04:06:49
  1. 安装 readline-devel 包。
  2. 使用 readline 模块重新编译 python
  3. Bingo!
  1. install readline-devel package.
  2. recompile python with readline module
  3. Bingo!
预谋 2024-07-27 04:06:49

以下是在 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) 执行以下命令:

./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
棒棒糖 2024-07-27 04:06:49

将 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
三人与歌 2024-07-27 04:06:49

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

-残月青衣踏尘吟 2024-07-27 04:06:49

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文