Emacs and conda workaround

What I found works for me is to use the conda package from ELPA and set two of its configuration variables to point to my Conda directory. The following snippet does the trick in my .emacs:

(use-package conda
  :ensure t
  :init
  (setq conda-anaconda-home (expand-file-name "~/miniconda3"))
  (setq conda-env-home-directory (expand-file-name "~/miniconda3")))
  • conda-anaconda-home is the equivalent to the ANACONDA_HOME environment variable (i.e. contains all files of your Anaconda installation)
  • conda-env-home-directory - is the directory where your virtual environments get stored (within the envs subdirectory)

With this configuration I'm able to run M-x conda-env-activate and have access to all previously created envs.


Programs inherit the environment variables from the shell that spawned them. The way conda and virtualenv work is by overriding the shell's PATH variable. They do this so that the OS finds the new version of the app (conda's or virtualenv's) instead of the default one installed with the OS (Macs come with an ancient version of python).

So, what is happening here? If you start Emacs by double clicking on the OS icon it will inherit the default shell environment variables. So when you try to call a library that you installed with conda (or equivalently with virtualenv and pip), because you are using the default OS path, the OS is finding the default version of python (and crucially the default version's libraries). The default version of python is going to respond "I have no idea what library that is."

How to fix? One reliable way is to not start Emacs by double clicking on the OS Icon. Here is what I do most days:

1) start a console/terminal
2) switch to the conda environment `activate py37` 
    (or with virtualenv: `source .py37dev/bin/activate`)
3) start Emacs from that same shell that has the modified environment variables.  
    On a Mac its: `/Applications/Emacs.app/Contents/MacOS/Emacs` 
    (I use a installed version of Emacs on the Mac because the one that 
    comes with Mac is ancient).  
    On Linux and Windows the path to EMacs will be different but the idea is the same.
4) start a shell inside Emacs and you should see the shell looks the way it does 
    in your conda shell (or virtualenv shell)

here it what it looks like for me: enter image description here

see how the version of python is not the default OS python? Its the one from the virtualenv + pip environment (conda works the exact same way, just the start envirmonment is a different command)