在 OS X 中的 Python shell 中无法输入“b”字母

在 OS X 中的 Python shell 中无法输入“b”字母

我在 OS X 中的 Python shell 中遇到了“b”字母问题。我无法输入“b”,但“B”可以正常输入。

我该如何解决这个问题?

答案1

您有问题的行.pythonstartup类似于:

 readline.parse_and_bind("bind ^I rl_complete") # darwin libedit

.pythonstartup将修复它...

try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    if 'libedit' in readline.__doc__:
        readline.parse_and_bind("bind ^I rl_complete")
    else:
        readline.parse_and_bind("tab: complete")

答案2

首先,直到我将 Python 2.7.1 更新到 2.7.3 后,这种情况才发生。也就是说,修复方法如下:

旧行:

if(sys.platform == 'darwin'): #FIX

新队:

if(sys.platform == 'darwin') and 'libedit' in readline.__doc__: #FIX

我的 ~/.pythonrc 中的完整代码

import atexit
import os
try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    import sys
    if(sys.platform == 'darwin') and 'libedit' in readline.__doc__: #FIX
    # OSX
        readline.parse_and_bind ("bind ^I rl_complete")
    else:
    # Linux
        readline.parse_and_bind("tab: complete")

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del atexit, save_history, historyPath

答案3

我也遇到了同样的问题。在 Snow Leopard 中使用 MacPorts 版本的 Python 时会发生这种情况。

我在 Mac OS X 附带的 Apple Python 中没有看到这个问题。因此,解决方法应该是设置PYTHONPATH指向 MacPorts 的包:

/opt/local/lib/python/site-packages:/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/ 

(或不同的 Python 版本)并使用 Apple 的 Python。

答案4

不幸的是,这个解决方法并不明智。

Apple 假定在执行升级和其他功能时其版本不会改变。相反,如果您希望升级 Python 或扩展它,建议您安装本地版本,无论是在/opt还是在您的主文件夹中。

我也有同样的问题,而且我没有使用MacPorts版本的python。


我正在使用最新版本的vpythonvisual python,来自www.vpython.org在最新的 MacBook Air(第三代)中运行的 Mac OX X Lion 下。

我使用他们最新的二进制文件并按照他们的说明安装了 vpython。它附带一个安装程序,因此只需点击一下即可。这是 Python 2.7.1 的修改版本。它是 32 位版本。(我相信他们还没有将其移植到 64 位)。然后我安装了 VPython-Mac-Py2.7-5.71。我随后安装了 scipywww.scipy.org和 matplotlib 来自matplotlib.sourceforge.net所有这些安装都使用安装程序。

当我从他们的开发工具 idle 或 vpython 变体 vidle 运行 python 时,我没有遇到任何问题。如果我打开终端并从 bash shell 运行 python,shell 将无法识别键盘上的“b”键。它会发出“铃声”,而不是输入“字符 b”。但是,您可以输入字母“B”。看起来这个键被映射到一些不正确的未显示的“字符”,可能是旧 ASCII 代码中的“铃声”字符。

我尝试将模拟器改为 xterm、vt100、vt102。我还尝试了不同的编码方案,比如只使用 UTF-8。我还按下了不同的特殊键组合,比如 command-b 等。但都不起作用。

我唯一的解决方法是编写空闲或空闲脚本。

我希望这有助于澄清这个问题。

相关内容