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?

2 Answers 2

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.

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 Feb 5, 2016 at 22:55
  • On which matter? On writing configs or on pyvenv itself?
    – immerrr
    Feb 6, 2016 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") Jun 20, 2018 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, 2020 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. Mar 18, 2021 at 13:16
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)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.