Search code examples
pythonemacselpy

Emacs Python Interpreter Is Not Set Correctly


Fresh install of Linux Mint 18.3 Cinnamon.

Also a fresh install of Gnu Emacs 24.5.1

Also a fresh install of Anaconda3:

 ~ $ conda -version
usage: conda [-h] [-V] command ...
conda: error: the following arguments are required: command
 ~ $ conda -V
conda 4.4.10
 ~ $ python -V
Python 3.6.4 :: Anaconda, Inc.
 ~ $ anaconda -V
anaconda Command line client (version 1.6.9

Emacs has python-mode, python, and elpy installed, python code executed via ctrl+Enter is run with the executable in /usr/bin/python which is hopelessly out of date...2.7.2

Executing python on the command line

 ~ $ which python
/home/user/anaconda3/bin/python
 ~ $ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)

Also, M-x elpy-config yields an error: neither easy install nor pip can be found very strange, given pip was installed with anaconda3

I don't know how to find what command elpy is using to run python or why elpy-config won't work, I assume that somewhere, it is hardcoded to the /usr/bin directory to look for all of the above executables, but no place I look indicates this.


Solution

  • This is what I suspect happened.

    When you configure $PATH, there are a few different ways to do it. One of the most common ways to do it is by adding it to your .bashrc (or .zshrc, .profile, whatever) file in your home directory. However, when you do this, this does not actually set $PATH anywhere else... only for the shell itself, and for programs runn from the shell.

    You can still launch programs from your window manager (e.g. Gnome, Cinnamon, KDE), and these programs will inherit $PATH not from your shell, but from your X login session.

    So after updating $PATH in .bashrc, Emacs will not see that path because it's not being launched from Bash.

    ASCII Art Time

         X Session $PATH=<original $PATH>
               + +
     +---------+ +--------+
     v                    v
    Terminal             Emacs $PATH=<original $PATH>
     +
     |
     v
    bash
    $PATH=/home/user/anaconda3/bin:$PATH
    

    Solution #1: Change $PATH in .emacs

    This is pretty easy. Just add a line to your .emacs near the top like this:

    (setenv "PATH" "/home/user/anaconda3/bin:/home/user/bin:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/game")
    

    This has some drawbacks... because now you're setting $PATH in two different places, and these two places can get out of sync with each other (you can forget to update one when the other changes).

    This is the solution I use.

    Solution #2: Configure Elpy to use your Python

    Elpy, like most Emacs packages, is configurable. See: https://emacs.stackexchange.com/questions/16637/how-to-set-up-elpy-to-use-python3

    You might be able to use M-x customize-group "elpy" or something like that, which means you don't have to edit your .emacs by hand.

    Solution #3: Change $PATH for your X session

    Depending on the specifics of your setup there are different ways to do this. I believe moving the $PATH definitions from .bashrc to .profile may work, but it's been a while since I've done this.