10

I'm using emacs and anaconda.

I have this in my init.el:

(setenv "WORKON_HOME" "/home/user/anaconda3/envs/")

And conda on my path:

# added by Anaconda3 installer
export PATH="/home/user/anaconda3/bin:$PATH"

but emacs can't find my conda environments, which I understand it is supposed to be able to do..

So, when I run C-c C-p to start a new session, and C-c C-c, it fails to import my packages which are installed in a conda environment, with ModuleNotFoundError.

Since I have added this to my path and it still doesn't work, I am trying to work around this, and still be able to run my conda applications from emacs.

I can open a shell in emacs with M-x shell, then source activate myenv, and run python.

I now want C-c C-c to copy into /this/ shell. How do I mark this shell buffer as a python process to send my file.py's text to on C-c C-c, rather than just a shell shell?

Update1

I've also looked at the following references:

But neither package works for me. I still get, when I try:

conda-env-list

*Conda envs*

Produces a blank buffer.

And this for pyvenv-workon:

pyvenv-workon
  Work on:  (empty)

These environments very much exist, and it makes it impossible to use emacs as a python IDE if I can't run my code.

1
  • 1
    Not completely sure, but look at Emacs' exec-path variable, this is where Emacs is searching for programs to run. As for loading a virtual environment, what works for me is calling pyvenv-activate.
    – amitr
    Mar 15, 2019 at 6:12

5 Answers 5

12

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.

6
  • This did it for me. Thanks! Jun 23, 2019 at 17:18
  • Thanks, it worked for me, but now, when activating a env, it says Searching for program: No such file or directory, python after running run-python, it seems not to be finding python.exe withing the env. Aug 13, 2019 at 8:00
  • 1
    @ElBaulP, sounds like a PATH problem. Try adding something like this to your .emacs: (add-to-list 'exec-path "~/miniconda3/bin") (setenv "PATH" "~/miniconda3/bin:$PATH" '("PATH")) Aug 13, 2019 at 8:03
  • @WojciechGac Thanks!, I do not have a bin/ subdirectory, but ~/miniconda3 worked, as python.exe is within it. Aug 13, 2019 at 8:49
  • @WojciechGac Thanks a lot! My problem was that I had the (setq conda-env-home-directory ...) under :config instead of :init. Thank you! Your post brought me to the problem. Jan 12, 2022 at 0:01
8

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)

1
  • 2
    How do you manage switching between different python projects that may be in different conda environments? Mar 15, 2019 at 22:20
0

I tested the solutions given in the answers of Wojciech Gac, of Mittenchops and James Anderson.

While solution of James Anderson's solution is by far the easiest, it comes with a few problems: First, you have to reactivate the environment in each shell process you spawn in emacs. There is also the possibility that emacs has a different pythonpath, therefor reluctantly using system python and not venv python.

The solution with conda.el is somehow weird. In Melpa it is listed as obsolete and with instructions of https://github.com/necaris/conda.el it won't recognize the environments on my certain machine.

On the same machine the solution with pyenv as mentioned in https://emacs.stackexchange.com/a/20093/28567 is working like a charm.

Therefor you only need to install with M-x package-install search for pyenv and then insert following two lines into .emacs:

(setenv "WORKON_HOME" "~/anaconda3/envs") ; /anaconda3 || /miniconda || wathever path your conda installation is located at
(pyvenv-mode 1)
0

This is my minimal solution to this problem:

create a batch file like this

conda activate <yourEnv>
python -i

set (local) python-shell-interpreter pointing to the batch-file

run-python as always (C-c C-p ...)

0

I know this is not exactly an answer to your question. But If you JUST want to run your code, open the directory in the terminal where your files are located (e.g. cd /Downloads/Chapter01/yourfile.py) . Then, activate the environment (conda activate *your env*) and pass this command (python yourfile.py). Your code will be executed within that environment by python. I edit remote files with emacs and run code like this in another terminal window. You may wanna open the two terminal windows side by side while debugging your code.

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.