Changing python interpreter for emacs

12,243

Solution 1

It is good habit to check customize-group of things you wanna tweak. Just do:

M-x customize-group RET python RET

you've got now multiple options of which one should be interesting:

Python Python Command

You can customize it there and Save for further sessions.

Solution 2

Via .emacs:

Try to add to your ~/.emacs file:

(setq python-python-command "~/your/python/bin-dir/python")

or

Via the shell environment:

The python command that is run by Emacs is typically python, so you can try the simple approach of changing your path:

export PATH=~/your/python/bin-dir:$PATH

Solution 3

python-python-command is for the older "loveshack python.el". For recent versions of Emacs, which use "gallina python.el", use the variable python-shell-interpreter.

(setq python-shell-interpreter "/path/to/python")

https://www.emacswiki.org/emacs/PythonProgrammingInEmacs#toc2

Share:
12,243
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin about 2 years

    Emacs uses an older version of python(2.3) i have for the default python mode, is there a way for me to tell emacs to use the newer version that i have in my home directory?

    btw I'm using a red hat distro and dont have root privileges.

  • mlt
    mlt almost 12 years
    Is there a way to switch dynamically? Like I have several python projects using different interpreters. It would be nice if there is an association possible between buffer and particular interpreter via local variable or something.
  • Bleeding Fingers
    Bleeding Fingers over 11 years
    @mit did you find an answer to that? I have been ferreting the internet for an answer but in vain. Although I know its quite possible through comint or babel and adding hooks, but the process seem quite convoluted and I have almost no experience in Lisp programming
  • PascalVKooten
    PascalVKooten almost 11 years
    This is the real answer. I just switched python to python3 and all my problems were immediately solved. Also found out the switch to turn on eldoc. Thanks!
  • Philippe
    Philippe over 9 years
    finally! Had been stumbling upon all these setq python-python-command suggestions that don't work. This is it. @sam, if it worked for you, you should accept the answer and mark it as solved
  • Philippe
    Philippe over 9 years
    on my other pc i had to find the "Python Shell Interpreter" line and change it
  • agent18
    agent18 about 6 years
    Doesn't work by adding to ~/.emacs.d/init.el file. Still opens the old interpreter when I say `M-x run-python'
  • jpkotta
    jpkotta about 6 years
    python-python-command is for "loveshack python.el", which was the python mode that shipped with older emacs versions. "Gallina python.el" is the one that's in current versions. Use python-shell-interpreter instead. emacswiki.org/emacs/PythonProgrammingInEmacs#toc2
  • agent18
    agent18 about 6 years
    The above didn't work for me in Emacs 27. Refer here for more information.
  • scry
    scry over 5 years
    This seems to have changed in a recent (25?) version of Emacs as @Philippe noticed
  • Lester Cheung
    Lester Cheung over 2 years
    Or add a per-file variable: # -*- python-shell-interpreter: "python3" -*-
  • Lester Cheung
    Lester Cheung over 2 years
    @mit look up file-variables but basically add something like this at first few lines of your main script # -*- python-shell-interpreter: "python3" -*- .