Python-通过shell scrip激活conda env

2024-04-30 00:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望运行一个简单的shell脚本来简化一些conda环境的管理。通过conda activatelinux操作系统中激活conda环境在shell中工作良好,但在shell脚本中有问题。有人能告诉我为什么会这样吗?

重复此问题的示例:

# default conda env
$ conda info|egrep "conda version|active environment"
     active environment : base
          conda version : 4.6.9

# activate new env to prove that it works
$ conda activate scratch
$ conda info|egrep "conda version|active environment"
     active environment : scratch
          conda version : 4.6.9

# revert back to my original conda env
$ conda activate base 

$ cat shell_script.sh
#!/bin/bash
conda activate scratch

# run shell script - this will produce an error even though it succeeded above
$ ./shell_script.sh

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

Tags: toinfoenv脚本environment环境initversion
2条回答

快速解决bash将下面的init脚本前置到Bash脚本中。

eval "$(command conda 'shell.bash' 'hook' 2> /dev/null)"

完成。


对于其他shell,请检查shell的init conf,在shell conf中复制以下内容并将其预处理到脚本中。

# >>> conda initialize >>>
...
# <<< conda initialize <<<

你也可以使用

conda init --all --dry-run --verbose

获取脚本中所需的init脚本。

解释

这与在conda4.6中引入conda init有关。

Quote from conda 4.6 release log

Conda 4.4 allowed “conda activate envname”. The problem was that setting up your shell to use this new feature was not always straightforward. Conda 4.6 adds extensive initialization support so that more shells than ever before can use the new “conda activate” command. For more information, read the output from “conda init –help”

conda4.6中引入conda init之后,conda only expose命令 conda进入PATH但不是“base”中的所有二进制文件。所有平台上的conda activate env-nameconda deactivate统一了环境切换。

但是要使这些新命令工作,必须使用conda init进行额外的初始化。

问题是脚本文件在子shell中运行,而conda在此子shell中未初始化。

参考文献

我使用“source command”运行shell脚本,它可以:

source shell_script.sh

错误消息非常有用-它告诉您conda没有从脚本运行所在的子shell中正确设置。要在脚本中使用conda,您将需要(如错误消息所示)首先运行conda init bash(或任何shell)。conda的行为及其设置方式取决于conda版本,但版本4.4+行为的原因是conda依赖于conda shell本身通常设置的某些环境变量。最重要的是,this changelog entry解释了为什么您的conda activatedeactivate命令不再像您在4.4及更高版本中期望的那样工作。

有关这方面的更多讨论,请参见GitHub上的official conda issue


编辑:更多的研究告诉我,错误消息中提到的conda init函数实际上是一个新的v4.6.0特性,它允许快速设置环境,以便您可以使用conda activate,而不是旧的source activate。但是,这之所以有效,是因为它添加/更改了当前shell的多个环境变量,并且还更改了RC文件(例如:.bashrc),并且RC文件的更改永远不会在当前shell中进行—只在新创建的shell中进行。(当然,除非你再次引用.bashrc)。事实上,conda init --help也这么说:

IMPORTANT: After running conda init, most shells will need to be closed and restarted for changes to take effect

但是,您显然已经运行了conda init,因为您可以交互使用conda activate。事实上,如果您打开.bashrc,您应该能够看到conda添加的几行代码,它们教您的shell在哪里查找conda命令。不过,脚本的问题在于,.bashrc是而不是源于运行shell脚本的子shell(有关更多信息,请参见this answer)。这意味着,即使您的非登录交互式shell看到conda命令,您的非交互式脚本子shell也不会-无论您调用conda init多少次。

这导致了一个猜想(我自己在Linux上没有conda,所以我无法测试它),通过这样运行您的脚本:

bash -i shell_script.sh

您应该看到conda activate工作正常。为什么?-i是一个bash标志,它告诉shell您开始在交互模式下运行,这意味着它将自动为您的.bashrc提供源代码。这应该足以让您在脚本中使用conda,就像您正常使用它一样。

相关问题 更多 >