43

我在 OS X (El Capitan) 上使用 Python 劣质 shell,每次我向 Python 进程发送一段代码(使用C-c C-r绑定到python-shell-send-region)时,我都会在 Python shell 中收到如下消息:

>>> import codecs, os;
    __pyfile = codecs.open('''/var/folders/6j/g9hcs1s17g1dgxr3p_m04rq80000gn/T/py5013kfY''', encoding='''utf-8''');
    __code = __pyfile.read().encode('''utf-8''');
    __pyfile.close();
    os.remove('''/var/folders/6j/g9hcs1s17g1dgxr3p_m04rq80000gn/T/py5013kfY''');
    exec(compile(__code, '''/Users/user/Desktop/sample.py''', 'exec'));

我不希望每次调用命令时都显示此消息。Linux 上相同版本的 Emacs(连同 python.el 文件)没有这个问题。消息的模板是在python-shell-send-file函数(在python.el文件中)中定义的,这本身就令人费解,因为我看不到在调用python-shell-send-region. 对于第一次运行,我使用以下命令调用 Python 解释器(版本 2.7.10)

/usr/bin/python -i

但它与其他 python 解释器(通过 brew 安装)的行为也相同。我正在使用 Emacs 24.5.1(再次通过 brew 安装)。我使用的唯一 python 模式特定配置.emacs与选项卡宽度有关:

(add-hook 'python-mode-hook
      (lambda ()
        (setq indent-tabs-mode t)
        (setq tab-width 2)
        (setq python-indent 2)))

任何帮助或建议表示赞赏。

编辑和可能的解决方案

(我发现这个https://github.com/jorgenschaefer/elpy/issues/1307):

$ easy_install-3.6 gnureadline
$ cd /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload
$ mv readline.cpython-36m-darwin.so readline.cpython-36m-darwin.so.bak

如果您在安装 gnureadline 时遇到错误,请fatal error: 'stdio.h' file not found尝试xcode-select --install.

4

1 回答 1

1
$ source ./your/venv/activate
$ pip install pyreadline

应该自动与Python一起工作。今年早些时候我在 Windows 10 上遇到了类似的问题,安装pyreadline作为开发依赖项解决了它。

于 2020-01-13T18:29:46.010 回答