21

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?

21

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.

| improve this answer | |
  • 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 5 '16 at 22:55
  • On which matter? On writing configs or on pyvenv itself? – immerrr Feb 6 '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 at 18:24
1

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)))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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