Emacs python-mode 的自定义 Python Shell

6 投票
3 回答
1389 浏览
提问于 2025-04-16 19:08

要在Emacs的python模式中,把普通的/usr/bin/python换成一个自定义的python shell,需要做些什么呢?

简单来说,我有一个二进制文件/usr/bin/mypython,它在启动python解释器之前会做一些初始化工作。对于所有的交互操作来说,这个解释器的功能和/usr/bin/python是一样的。

不过,如果我在"python-python-command"中指定这个二进制文件(在Emacs 23.3中使用python.el),我会收到一个提示:“只支持Python版本>=2.2且<3.0”。

3 个回答

0

在使用python-mode.el的时候,只需要这样做:

M-x MY-PYTHON 然后按回车键

前提是你已经安装了Python版本

https://launchpad.net/python-mode

1

最简单的方法来绕过这个检查就是撒谎,告诉 python-check-version 它已经被检查过了:

(setq python-version-checked t)
5

我准备去看看elisp的代码,不过我敢打赌,如果你加上一个 --version 的参数,让它返回 /usr/bin/python 的结果,emacs就会很开心了。

更新

这是EMACS 23.3.1中python.el文件第1555行及之后的代码:

(defvar python-version-checked nil)
(defun python-check-version (cmd)
  "Check that CMD runs a suitable version of Python."
  ;; Fixme:  Check on Jython.
  (unless (or python-version-checked
          (equal 0 (string-match (regexp-quote python-python-command)
                     cmd)))
    (unless (shell-command-to-string cmd)
      (error "Can't run Python command `%s'" cmd))
    (let* ((res (shell-command-to-string
                 (concat cmd
                         " -c \"from sys import version_info;\
print version_info >= (2, 2) and version_info < (3, 0)\""))))
      (unless (string-match "True" res)
    (error "Only Python versions >= 2.2 and < 3.0 are supported")))
    (setq python-version-checked t)))

它的作用是运行一行简单的代码

from sys import version_info;
print version_info >= (2, 2) and version_info < (3, 0)

只会打印出“True”或“False”。你只需要修改你的脚本,让它能处理 -c 参数,这样就没问题了。

另外,你也可以采取一种黑客的方式,把 python-version-checked 的值强制设为 t,这样它就永远不会进行检查了。

撰写回答