Emacs中的Python 2和3

17 python emacs python-3.x

我一直在使用Emacs编写Python 2代码.现在我在我的系统上安装了Python 2.6和3.0,我也需要编写Python 3代码.

以下是在/ usr/bin中设置不同版本的方法:

python -> python2.6*
python2 -> python2.6*
python2.6*

python3 -> python3.0*
python3.0*
Run Code Online (Sandbox Code Playgroud)

有没有办法设置它,以便Emacs使用正确版本的Python,具体取决于我使用的语言?例如,Cc Cc当前运行缓冲区,但它总是调用python2.6,即使我正在编写Python 3代码.

cef*_*tat 10

如果您使用的是python-mode.el,可以尝试更改py-which-shell.为了在每个文件的基础上执行此操作,您可以放置

# -*- py-which-shell: "python3"; -*-
Run Code Online (Sandbox Code Playgroud)

在文件的第一行 - 如果第一行以第二行开头,则在第二行#!.另一个选择是放

# Local Variables:
# py-which-shell: "python3" 
# End: 
Run Code Online (Sandbox Code Playgroud)

在你的文件的末尾.也许你应该给python3的完整路径而不仅仅是"python3".


jro*_*way 4

答案是肯定的。如果你能区分 Python 2 和 Python 3,那么让 emacs 做你想做的事情就是一件简单的编程事情。

(define run-python (&optional buffer)
    (with-current-buffer (or buffer (current-buffer))
        (if (is-python3-p)
              (run-python3)
            (run-python2))))

(define-key python-mode-map (kbd "C-c C-c") #'run-python)
Run Code Online (Sandbox Code Playgroud)

剩下要做的就是实施is-python3-prun-python3(等等)