bash 如何在子 shell 中激活 conda env?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41914739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:40:19  来源:igfitidea点击:

How do I activate a conda env in a subshell?

pythonbashcondashebangsubshell

提问by meh

I've written a python program. And if I have a shebang like this one:

我写了一个python程序。如果我有这样的shebang:

#!/usr/bin/python

and I make the file executable with:

我使文件可执行:

$ chmod 755 program.py

I can run the program like so:

我可以像这样运行程序:

$ ./program.py

Here is the issue. I use the conda virtual environments. When I run the program like above, the system creates a subshell that does not recognize the active environment:

这是问题。我使用conda 虚拟环境。当我像上面一样运行程序时,系统会创建一个无法识别活动环境的子shell:

(my_env) $ ./program.py
ImportError: No module named pymongo

If I do it this way, however...

但是,如果我这样做,...

(my_env) $ python program.py
# blah blah... runs great

How do I specify the right environment for use in the subshell? Is it possible? I'd like to save my fingers the effort of typing the six character string that is python.

如何指定在子shell中使用的正确环境?是否可以?我想省去我的手指输入六个字符的工作量,即python.

Another post, Shebangs in conda managed environments, briefly touches on this but does not provide the right answer. Instead of activating the environment in the subshell, it just says, go ahead and ignore the shebang... just use the $ python program.pysyntax.

另一篇文章,在 conda 管理环境中的 Shebangs,简要介绍了这一点,但没有提供正确的答案。它没有在子shell中激活环境,而是说,继续并忽略shebang......只需使用$ python program.py语法。

采纳答案by merv

conda run

conda run

If you always plan to run the script from a shell session where condais defined, then another alternative is let Conda load the env using the conda runcommand. In this case, the shebang would be

如果您总是计划从conda定义的 shell 会话运行脚本,那么另一种选择是让 Conda 使用conda run命令加载 env 。在这种情况下,shebang 将是

#!/usr/bin/env conda run -n my_env python

The advantage here is that you don't need the env to be activated when you call ./program.pyand you don't have to hardcode the location of the interpreter.

这里的优点是您不需要在调用时激活 env./program.py并且您不必硬编码解释器的位置。

Note:This command was added as a "preview" in Conda v4.6.0 (see Release Notes) to address the issue of running a command inside an env.

注意:此命令在 Conda v4.6.0(请参阅发行说明)中作为“预览”添加,以解决在 env 中运行命令的问题

回答by Nehal J Wani

In your script, change...

在您的脚本中,更改...

#!/usr/bin/python

...to:

...到:

#!/usr/bin/env python

The python used by an activated conda environment is ${CONDA_PREFIX}/bin/pythonand not /usr/bin/python

激活的 conda 环境使用的 python 是${CONDA_PREFIX}/bin/python和不是/usr/bin/python

Notice the difference?

注意到区别了吗?

(root) ~/condaexpts$ which python
/home/ubuntu/condaexpts/m3/bin/python

(root) ~/condaexpts$ /usr/bin/env python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

(root) ~/condaexpts$ source deactivate

~/condaexpts$ which python
/usr/bin/python

~/condaexpts$ /usr/bin/env python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

回答by Dave X

You can also point directly to the environment with the shebang line so you need not depend on something setting up the parent environment prior to calling the script.

您还可以使用 shebang 行直接指向环境,因此您无需在调用脚本之前依赖于设置父环境的某些内容。

First determine your preferred python environment's path:

首先确定您首选的python环境的路径:

$ . activate mypython
$ which python
/home/username/anaconda/envs/mypython/bin/python      # for example 

Then use it in a script:

然后在脚本中使用它:

#!/home/username/anaconda/envs/mypython/bin/python
import os,sys
print sys.executable
print os.__file__

The above script would give output like this:

上面的脚本会给出这样的输出:

/home/username/anaconda/envs/mypython/bin/python
/home/username/anaconda/envs/mypython/lib/python2.7/os.pyc

Having a non-generic command in the shebang makes the script less portable, but if you depend the specific packages in a particular virtual environment, this is what you want.

在 shebang 中使用非通用命令会降低脚本的可移植性,但是如果您依赖特定虚拟环境中的特定包,这就是您想要的。