Emacs and conda workaround

后端 未结 4 1487
暖寄归人
暖寄归人 2021-01-18 05:01

I\'m using emacs and anaconda.

I have this in my init.el:

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

And conda on my p

相关标签:
4条回答
  • 2021-01-18 05:40

    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.

    0 讨论(0)
  • 2021-01-18 05:42

    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:

    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)

    0 讨论(0)
  • 2021-01-18 05:46

    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 讨论(0)
  • 2021-01-18 05:54

    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 讨论(0)
提交回复
热议问题