简体   繁体   English

Python 交互模式历史和方向键

[英]Python interactive mode history and arrow keys

I was wondering if anyone can explain why all of a sudden in Python interactive mode all arrow keys are failing?我想知道是否有人可以解释为什么突然在 Python 交互模式下所有箭头键都失效了?

When I press up button for example to go through command history I get "^[[A".例如,当我通过命令历史按下向上按钮到 go 时,我得到“^[[A”。 Same with any other arrow keys.与任何其他箭头键相同。

I have no idea why this happened and it was working before (on OS X Snow Leopard).我不知道为什么会发生这种情况,并且它之前可以正常工作(在 OS X Snow Leopard 上)。 Does anyone know a way to fix this?有谁知道解决这个问题的方法?

Many thanks,非常感谢,

G G

If you are using homebrew, this is an easy fix: 如果您使用的是自制软件,这是一个简单的解决方法:

brew uninstall python
brew uninstall readline
brew install readline  --universal
brew install python

That fixed it for me (running OS X Mavericks 10.9.5) 为我修复它(运行OS X Mavericks 10.9.5)

I finally got this working. 我终于搞定了这个。 I just had to install readline with easy_install and cursors and backspace started magically working. 我只需要使用easy_install和游标安装readline,并且退格开始神奇地工作。

sudo /opt/local/bin/easy_install-2.5 readline

You don't say which Python you are using but the symptoms you mention are indeed usually caused by Python not being built with readline support. 你没有说你正在使用哪种Python,但你提到的症状确实通常是由Python没有使用readline支持构建的。 These days Python on OS X can be built to use either the GNU readline library or the Apple-supplied editline library (AKA libedit ). 目前,OS X上的Python可以构建为使用GNU readline库或Apple提供的editline库(AKA libedit )。 You can use the following two commands to show exactly which Python you are using. 您可以使用以下两个命令来准确显示您正在使用的Python。 If that does not help you figure out what is going on, edit your question to show the output from those commands. 如果这无法帮助您弄清楚发生了什么,请编辑您的问题以显示这些命令的输出。

Here's an example that shows a recent MacPorts Python 2.6 on OS X 10.6: 这是一个示例,显示OS X 10.6上最近的MacPorts Python 2.6:

$ python -c 'import sys;print(sys.version);print(sys.executable)'
2.6.5 (r265:79063, Jul 15 2010, 01:53:46) 
[GCC 4.2.1 (Apple Inc. build 5659)]
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python

$ otool -L $(python -c 'import readline; print(readline.__file__)')
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/readline.so:
    /opt/local/lib/libreadline.6.1.dylib (compatibility version 6.0.0, current version 6.1.0)
    /opt/local/lib/libncursesw.5.dylib (compatibility version 5.0.0, current version 5.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)

The path prefix /opt/local/ is the default location for MacPorts-installed software and the output from otool indicates that this Python's readline module is dynamically linked to the MacPorts-installed GNU readline library. 路径前缀/opt/local/是MacPorts安装软件的默认位置, otool的输出表示此Python的readline模块动态链接到安装了MacPorts的GNU readline库。

This behaviour commonly shows when you do not have readline support. 当您没有readline支持时,通常会显示此行为。 If you are using MacPorts, try port install readline , see if it will fix it. 如果您使用的是MacPorts,请尝试使用port install readline ,看看它是否会修复它。 You can also see this page for some further explanations. 您还可以查看此页面以获得进一步的解释。

(Also useful to know: some programs do not use readline even if present on the system. You can force it on them by using rlwrap ( port install rlwrap ). For example: rlwrap ocaml -init code.ml will start up OCaml, read code.ml, and start REPL with readline support) (也很有用:一些程序即使存在于系统上也不使用readline 。你可以使用rlwrapport install rlwrap )强制它。例如: rlwrap ocaml -init code.ml将启动OCaml,读取code.ml,并启动REPL并支持readline)

The command命令

brew install readline

worked for me.为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM