5

I've got code that needs python 2.7 (and other environments)

C:\Users\Me>conda info -e
# conda environments:
#
base                  *  C:\Program Files\Anaconda3
python2                  C:\Program Files\Anaconda3\envs\python2

I would like to be able to selectively activate python2 when I start a python shell in emacs (i.e. I send a line of code to the python interpreter using elpy). being able to put this in an init file would be cool.

I have tried both the conda and pyenv packages with no luck in my current configuration. Conda can't find my environments and pyenv doesn't seem to actually activate the virtual environment.

Using Emacs 26.1 and conda 4.5.11

Any help would be appreciated.

user2127595
  • 81
  • 1
  • 3
  • I have the same issue,anyone have a better solution to activate conda virtual envirenment in Emacs. I use Anaconda-mode for company. when I use `C-c , C-p` to run python,it always gives the warning below: Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32 Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation Type "help", "copyright", "credits" or "license" for more information. my setting is as below, – Eason Feb 25 '20 at 08:46

3 Answers3

3

Activate your virtualenv with:

(pyvenv-activate "C:\Program Files\Anaconda3\envs\python2")

If the binaries in your virtualenv are names "python2", you will need to set

(setq python-shell-interpreter "python2")

to ensure the good python binaries will be used by Elpy.

galaunay
  • 31
  • 3
2

You can use conda.el to manage your conda environments within python.

After install and setup, do M-x conda activate python2

al0
  • 151
  • 7
1

I use pyvenv to activate/manage my virtual environments and have the following called from my init.el...

(elpy-enable)
(pyvenv-activate "~/.virtualenvs/dskit")

I have installed virtualenvwrapper which resides in ~/.virtualenvs and creates the virtualenvs within there (e.g. dskit that is being activated). As you are using Conda change ~/.virtualenvs/dskit to point to the location of the Conda environment you wish to use. Obviously you then need to install the Emacs pyvenv package (I use the latest version from MELPA).

I'm by no means an expert in either Python, Virtual Envs or Emacs and used the following articles from realpython.com....

slackline
  • 293
  • 3
  • 14
  • 1
    `pyvenv` is maintained by the author of `elpy`. Installing `elpy` will automatically install `pyvenv` as a dependency. A key component to `elpy` is `elpy-config`. It tells you what virtual environment is being used, if any. It tells you which Python executable is being used for RPCs (remote procedure calls). And finally, it let's you know what Python executable is being used for the interactive Python shell. Finding `elpy-config` was the "aha" moment for me. I highly recommend the pdf manual (I find it easier to read): https://media.readthedocs.org/pdf/elpy/latest/elpy.pdf – Lorem Ipsum Feb 06 '19 at 14:18