2

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.

user2127595
  • 176
  • 14
  • Run `type python` in your console. What is `$PATH`? Is it propagated to Emacs? – Dietrich Epp Apr 09 '18 at 20:55
  • Don't worry about the second error with "…nor pip can be found"; that's just a natural consequence of the first one: if it's finding your Python 2.7.2, and you don't have pip for that Python 2.7.2, it can't find your pip. (Which is a good thing; it would get hopelessly confused if it were running one python and an unrelated pip…) – abarnert Apr 09 '18 at 20:58
  • @Prateek: You might be thinking of Windows. – Dietrich Epp Apr 09 '18 at 20:59
  • `which python` yields `/home/user/anaconda3/bin/python`, Path is: `/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` – user2127595 Apr 09 '18 at 21:00
  • `which pip` yields: `/home/user/anaconda3/bin/pip` – user2127595 Apr 09 '18 at 21:02
  • You shouldn't need to set [`elpy-rpc-python-command`](https://elpy.readthedocs.io/en/latest/concepts.html#option-elpy-rpc-python-command) or `elpy-rpc-pythonpath`, but for debugging purposes, try setting them (separately and both together) and see if that works. Also try just using `python3` instead of the full path to the Anaconda Python 3. – abarnert Apr 09 '18 at 21:02
  • 1
    What happens when you run `(getenv "PATH")` in Emacs? Type it in your `*scratch*` buffer and then press C-j to run it and see the output. – Dietrich Epp Apr 09 '18 at 21:07
  • @Prateek: On Linux, executable files do not generally end with `.exe`. The file's name will be `pip`. – Dietrich Epp Apr 09 '18 at 21:09
  • @DietrichEpp Holy shit, it has the old path, prior to adding anaconda. I have restarted emacs many times after the installation, so it's cached somewhere? – user2127595 Apr 09 '18 at 21:09

2 Answers2

2

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.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • As expected, Anaconda3 appends the anaconda directory within my `.bashrc` rather than anyplace lower down... I'm partial to option 1, though I'm going to do a little research to see if there's a good way of getting emacs to include `.bashrc` or `.profile` rather than hard coding. – user2127595 Apr 09 '18 at 21:31
  • From this [answer](https://emacs.stackexchange.com/questions/14159/why-emacs-overrides-my-path-when-it-runs-bash) Steve Purcell's package [exec-path-from-shell](https://github.com/purcell/exec-path-from-shell) is a nice workaround. – user2127595 Apr 09 '18 at 21:39
  • In more detail, when you install that package, every time you open a shell from in emacs (including a python shell), it pulls in any local configuration. I installed this package without making any other changes and found my issue resolved, elpy launches python 3.6.4 – user2127595 Apr 09 '18 at 21:41
0

The accepted answer has a nice explanation of what's going on.

For a quick solution, Install this package: exec-path-from-shell

It ensures that whenever emacs launches a shell to run something, it will use your local shell configuration including modifications made to $PATH in .bashrc

user2127595
  • 176
  • 14