8

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.

0
1

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")
6

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

1
  • Or by M-x customise[RET]python-shell-interpreter[RET] from inside emacs.
    – Grimxn
    Feb 17 at 17:52
0

For recent Python versions (3.3 or newer), I would advise you use the pylauncher py, instead of calling directly Python with python or python3. The pylauncher does exactly the same, but lets you specify which version you want to use amongst those installed on your system.

You can test it inside a terminal, by typing py -3.9 to launch version 3.9 of python, py -3.7 to launch its version 3.7, and so on. Of course, this will only work if you installed both versions on your system.

For Emacs run-python, this translates in using the following:

    (setq python-shell-interpreter "py")
    (setq python-shell-interpreter-args "-3.9")

For Microsoft Windows users, that would be something along thoses lines:

(setq python-shell-interpreter "py")
(setq python-shell-interpreter-args "-3.9 -i c:/Users/YOUR_PROFILE_NAME/AppData/Local/Continuum/anaconda3/Scripts/ipython-script.py")

If you run a dedicated run process (C-u C-c C-p), you can also choose dynamically which version of Python you want to run for the buffer. That makes it very easy to use Python 3.7 (which currently supports tensorflow) on a particular file, while using 3.9 (which currently doesn't) on all the others.

Source: How to run multiple Python versions on Windows

Your Answer

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