I'm trying to figure out how to specify which version of python to run when calling "run-python" (C-c C-p) in python-mode, and I've run into the following problem. These are the first few lines of the definition of run-python:

(defun run-python (cmd &optional dedicated show)

"Run an inferior Python process. Input and output via buffer named after `python-shell-buffer-name'. If there is a process already running in that buffer, just switch to it.

With argument, allows you to define CMD so you can edit the command used to call the interpreter and define DEDICATED, so a dedicated process for the current buffer is open. When numeric prefix arg is other than 0 or 4 do not SHOW.

I would like to specify the CMD argument so as to specify "python2" or "python3." For example, I've tried things like

M-: (run-python python2)

M-: (run-python (shell-command "/usr/bin/python2"))

but nothing works.

Edit: I figured out that if I supply 0 as a numeric argument, then there is a prompt where I can specify the python version. I would still like to know why neither of my attempts to specify CMD in the actual function call did not work though.


according to the contents of python.el (the source file of python mode) run-python's cmd defaults to a function which returns the path and parameters of the system's default python version, and in turn runs this via python-shell-make-comint, so simply setting an unquoted name (a nonexistant variable as ELisp sees it) or trying to run a shell command directly won't work.

What works, however, is M-: (run-python "python3"), using double quotes in comparison to your first variant. It opens a new buffer with a python prompt, but does not necessarily get it to the front (ie., use C-x C-b and select the python buffer from the list). You can see that it worked if „Shell native completion is enabled” appears in the status line.

Depending on which python version you usually run, it is even easier to set it in your ~/.emacs.d/init.el and run it via C-c C-p, or to add those two variables as file-locals in a comment block at the end of a python file (excerpt from python.el's header):

;; … .  You can change
;; your default interpreter and commandline arguments by setting the
;; `python-shell-interpreter' and `python-shell-interpreter-args'
;; variables.  This example enables IPython globally:
;; (setq python-shell-interpreter "ipython"
;;       python-shell-interpreter-args "-i")
| improve this answer | |

Put (setq python-shell-interpreter "python3") into your .emacs.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy