新人问如何改变EMACS的默认python版本呢?

大家好,请帮助一个新人的问题,

Ubuntu 18.04虚拟机原生环境,装好了python3.7,装好了Emacs,装好了Spacemacs,写了helloworld.py, C-c C-p运行python解释器,显示用的的是Ubuntu默认的python2.7. 请问怎么能变成python3.7呢?

感谢回复,请详细些,新人刚学python,刚学EMACS!

python-shell-interpreter,百度,google很容易得到的答案。

我搜索了一天了,没找到,能详细点吗,谢谢

Emacs 内置了丰富的文档,建议了解下 help 和 info ,有助于上手。

常用的查看 help 方法:

  • 查看函数: C-h f foo-function RET
  • 查看变量: C-h v foo-variable RET
  • 查看按键绑定的命令,比如查 C-c C-p 对应的命令: C-h k C-c C-p ,或者粗略地查看: C-h c C-c C-p
  • 查看执行历史: C-h l ,最近的在底部

以你这个问题为例,首先 C-h k C-c C-p 查看执行了什么命令:

C-c C-p runs the command run-python (found in python-mode-map), which
is an interactive autoloaded compiled Lisp function in ‘python.el’.

It is bound to C-c C-p, <menu-bar> <Python> <Start interpreter>.

(run-python &optional CMD DEDICATED SHOW)

Run an inferior Python process.

Argument CMD defaults to ‘python-shell-calculate-command’ return
value.  When called interactively with ‘prefix-arg’, it allows
the user to edit such value and choose whether the interpreter
should be DEDICATED for the current buffer.  When numeric prefix
arg is other than 0 or 4 do not SHOW.

For a given buffer and same values of DEDICATED, if a process is
already running for it, it will do nothing.  This means that if
the current buffer is using a global process, the user is still
able to switch it to use a dedicated one.

Runs the hook ‘inferior-python-mode-hook’ after
‘comint-mode-hook’ is run.  (Type C-h m in the
process buffer for a list of commands.)

[back]

里面提到此命令通过 python-shell-calculate-command 函数来得到实际运行命令的, 再 C-h f python-shell-calculate-command RET 看此函数的 help 文档 (实际此时可以直接点击 help buffer 中的链接,无须手动敲):

python-shell-calculate-command is a compiled Lisp function in
‘python.el’.

(python-shell-calculate-command)

Calculate the string used to execute the inferior Python process.

[back]

这个函数帮助信息不多,但注意顶部的 python.el 其实是一个链接,点击可以跳转到源码位置:

(defun python-shell-calculate-command ()
  "Calculate the string used to execute the inferior Python process."
  (format "%s %s"
          ;; `python-shell-make-comint' expects to be able to
          ;; `split-string-and-unquote' the result of this function.
          (combine-and-quote-strings (list python-shell-interpreter))
          python-shell-interpreter-args))

这里可以看到解释器是由 python-shell-interpreter 变量控制的, 尝试更改之: (setq python-shell-interpreter "/path/to/python3")

有时候可能没有这么幸运,那就再尝试搜索下 info ,比如: M-x info-apropos RET run-python RET

5 个赞

多谢了,感谢详细回复,请问如何修改这个源码,我找到了源码,但是是个压缩文件,解压缩,修改,然后再压缩?

不是,在自己的 spacemacs 配置中更改即可: (setq python-shell-interpreter "/path/to/python3") ( 我不用 spacemacs ,所以具体放到哪里你可能要去 spacemacs 中找下文档)。

如果 python3 已经在 load-path 中 (可以理解为 shell 的 PATH ) ,不需要写出完整路径。

感觉这样学习好像加大了学习难度。

我觉得应该是这样的:

emacs不配置的情况下学习入门操作,同时可以用python自带的idle学习python;

接着可以学习加入spacemacs配置后的emacs操作;

最后在spacemacs中折腾python。

(学霸请无视)

谢谢大家的回复,我刚才通过Pyenv解决了,原理虽然还没弄清楚,学习环境已经搞定了。多谢回复!

  (setq python-indent-offset 4
        python-sort-imports-on-save t
        python-shell-interpreter "python3"
        pippel-python-command "python3"
        importmagic-python-interpreter "python3"
        flycheck-python-pylint-executable "pylint"
        flycheck-python-flake8-executable "flake8")
  ;; if you use pyton2, then you could comment the following 2 lines
  ;; (setq python-shell-interpreter "python2"
  ;;       python-shell-interpreter-args "-i")

这个把我会用到的executable都改了一遍。都是pip3的

pyenv, pipenv配套的emacs 包都是比较方便的解决方式。

1 个赞

最好用google,百度找不到什么好的博客,因为很多不错的都是英文写的。

谢谢您教的思路!谢谢