Emacs and conda workaround

Mit*_*ops 6 python emacs conda

I'm using emacs and anaconda.

I have this in my init.el:

(setenv "WORKON_HOME" "/home/user/anaconda3/envs/")
Run Code Online (Sandbox Code Playgroud)

And conda on my path:

# added by Anaconda3 installer
export PATH="/home/user/anaconda3/bin:$PATH"
Run Code Online (Sandbox Code Playgroud)

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*
Run Code Online (Sandbox Code Playgroud)

Produces a blank buffer.

And this for pyvenv-workon:

pyvenv-workon
  Work on:  (empty)
Run Code Online (Sandbox Code Playgroud)

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

Woj*_*Gac 9

我发现对我有用的是使用conda来自 ELPA的包并将其两个配置变量设置为指向我的 Conda 目录。以下代码段在 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")))
Run Code Online (Sandbox Code Playgroud)
  • conda-anaconda-home相当于ANACONDA_HOME环境变量(即包含 Anaconda 安装的所有文件)
  • conda-env-home-directory- 是存储虚拟环境的目录(在envs子目录中)

使用此配置,我可以运行M-x conda-env-activate并访问所有以前创建的环境。


Jam*_*son 6

程序从产生它们的外壳程序继承环境变量。conda和virtualenv的工作方式是通过覆盖Shell的PATH变量。他们这样做是为了让OS查找该应用程序的新版本(conda或virtualenv),而不是随该操作系统安装的默认版本(Mac附带了一个古老的python)。

那么,这里发生了什么?如果通过双击OS图标启动Emacs,它将继承默认的Shell环境变量。因此,当您尝试调用使用conda(或等效地使用virtualenv和pip)安装的库时,由于您使用的是默认操作系统路径,因此操作系统会找到python的默认版本(以及至关重要的是默认版本的库)。python的默认版本将响应“我不知道它是什么库”。

怎么修?一种可靠的方法是通过双击OS图标启动Emacs。这是我大部分时间做的事情:

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)
Run Code Online (Sandbox Code Playgroud)

在这里对我来说是什么样子: 在此处输入图片说明

看看python版本不是默认的操作系统python吗?它来自virtualenv + pip环境(conda以完全相同的方式工作,只是start环境是不同的命令)

  • 您如何管理可能在不同conda环境中的不同python项目之间的切换? (2认同)