27

What is the best way to use conda environmets using emacs as a Python IDE?

I have got different conda environments while programming in Python:

$ conda info -e
# conda environments:
#
django                   /Users/Pablo/anaconda/envs/django
scipy                 *  /Users/Pablo/anaconda/envs/scipy
visual                   /Users/Pablo/anaconda/envs/visual
ml                       /Users/Pablo/anaconda/envs/ml
root                     /Users/Pablo/anaconda

But when I use crtl+c crtl+c emacs only uses my the Mac OS X default Python PATH. How can I chage it between all the different conda environments?

PabloRdrRbl
  • 435
  • 1
  • 4
  • 7

2 Answers2

26

I'd suggest using pyvenv library, it provides a neat interface to selecting a virtual env among several alternatives updating interpreter and library paths. You will need to alter WORKON_HOME variable (it defaults to $HOME/.virtualenvs used by virtualenvwrapper).

(setenv "WORKON_HOME" "/Users/Pablo/anaconda/envs")
(pyvenv-mode 1)

After that choose the environment with M-x pyvenv-workon.

immerrr
  • 554
  • 4
  • 6
  • It worked for me, thank you! Do you know where can I found more documentation on this matter? I haven't found any information about pyvenv and anaconda, but I added this two lines in my settings file and it's working fine. @immerrr – PabloRdrRbl Feb 05 '16 at 22:55
  • On which matter? On writing configs or on pyvenv itself? – immerrr Feb 06 '16 at 22:46
  • 1
    To switch between python3 and python2, I had to issue `(setq python-shell-interpreter "python3")` or `(setq python-shell-interpreter "python2")` – Lorem Ipsum Jun 20 '18 at 23:30
  • A bit more robust way of doing this is `(setenv "WORKON_HOME" (concat (getenv "CONDA_PREFIX") "/envs"))`. This way when you change machines but want to keep your emacs config file consistent. – D_Serg Mar 31 '20 at 18:24
  • I use the first method which means (setenv "WORKON_HOME" "/Users/Pablo/anaconda/envs") (pyvenv-mode 1) but when I run my python file, the default env is still used. – FightingGoWp Mar 18 '21 at 13:16
  • When juggling multiple conda environments, Instead of altering environment variables as this answer suggests, I set the conda environment based on the project I am in via `pyenv-activate` (in my case, setting it to `~/apps/miniconda/envs/my-conda-environment`, though your conda env path will likely differ, see value of `$CONDA_PREFIX` in terminal when env is activated). – holocronweaver Jun 14 '23 at 21:39
8

I have recently incorporated Anaconda into my python development and was having similar problems setting up Emacs + flycheck + linters. This answer got me up and running, but I've modified it, since the conda.el package is available. The following will integrate in the Anaconda path and setup Emacs to recognize the currently active Anaconda env. As a bonus, it updates the Mode line with the env name.

;;Anaconda support
(require 'conda)

(setq conda-env-home-directory "<path-to>/anaconda3")
;;get current environment--from environment variable CONDA_DEFAULT_ENV
(conda-env-activate 'getenv "CONDA_DEFAULT_ENV")
;;(conda-env-autoactivate-mode t)
(setq-default mode-line-format (cons mode-line-format '(:exec conda-env-current-name)))
Jim Parker
  • 181
  • 1
  • 2