Python - 通过 shell 脚本激活 conda env [英] Python - Activate conda env through shell script

查看:33
本文介绍了Python - 通过 shell 脚本激活 conda env的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望运行一个简单的 shell 脚本来简化一些 conda 环境的管理.在 linux 操作系统中通过 conda activate 激活 conda 环境在 shell 中工作正常,但在 shell 脚本中是有问题的.有人能指出我为什么会发生这种情况的正确方向吗?

I am hoping to run a simple shell script to ease the management around some conda environments. Activating conda environments via conda activate in a linux os works fine in the shell but is problematic within a shell script. Could someone point me into the right direction as to why this is happening?

重复问题的例子:

# 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'.

推荐答案

该错误消息很有帮助 - 它告诉您 conda 未从运行脚本的子 shell 中正确设置.为了能够使用conda 在脚本中,您需要(如错误消息所述)首先运行 conda init bash (或任何您的 shell).conda 的行为及其设置方式取决于您的 conda 版本,但 4.4+ 版本行为的原因是 conda 依赖于通常由 conda shell 本身设置的某些环境变量.最重要的是,此变更日志条目 解释了为什么您的 conda activatedeactivate 命令在 4.4 及更高版本中的行为不再符合您的预期.

The error message is rather helpful - it's telling you that conda is not properly set up from within the subshell that your script is running in. To be able to use conda within a script, you will need to (as the error message says) run conda init bash (or whatever your shell is) first. The behaviour of conda and how it's set up depends on your conda version, but the reason for the version 4.4+ behaviour is that conda is dependent on certain environment variables that are normally set up by the conda shell itself. Most importantly, this changelog entry explains why your conda activate and deactivate commands no longer behave as you expect in versions 4.4 and above.

有关此问题的更多讨论,请参阅 GitHub 上的官方 conda 问题.

For more discussion of this, see the official conda issue on GitHub.

更多的研究告诉我错误消息中提到的conda init函数实际上是一个新的v4.6.0功能,它允许快速设置环境,以便您可以使用condaactivate 而不是旧的 source activate.但是,这样做的原因是它添加/更改了您当前 shell 的几个环境变量,并且还更改了您的 RC 文件(例如:.bashrc),并且永远不会拾取 RC 文件更改在当前外壳中 - 仅在新创建的外壳中.(当然,除非您再次使用 .bashrc ).事实上,conda init --help 说了这么多:

Some more research tells me that the conda init function mentioned in the error message is actually a new v4.6.0 feature that allows a quick environment setup so that you can use conda activate instead of the old source activate. However, the reason why this works is that it adds/changes several environment variables of your current shell and also makes changes to your RC file (e.g.: .bashrc), and RC file changes are never picked up in the current shell - only in newly created shells. (Unless of course you source .bashrc again). In fact, conda init --help says as much:

重要提示:运行 conda init 后,大多数 shell 需要关闭并重新启动才能使更改生效

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 提供(参见 这个答案 了解更多信息).这意味着即使您的非登录交互式 shell 看到 conda 命令,您的非交互式脚本子 shell 也不会 - 无论您调用 conda init 多少次.

However, you've clearly already run conda init, because you are able to use conda activate interactively. In fact, if you open up your .bashrc, you should be able to see a few lines added by conda teaching your shell where to look for conda commands. The problem with your script, though, lies in the fact that the .bashrc is not sourced by the subshell that runs shell scripts (see this answer for more info). This means that even though your non-login interactive shell sees the conda commands, your non-interactive script subshells won't - no matter how many times you call conda init.

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

This leads to a conjecture (I don't have conda on Linux myself, so I can't test it) that by running your script like so:

bash -i shell_script.sh

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

you should see conda activate work correctly. Why? -i is a bash flag that tells the shell you're starting to run in interactive mode, which means it will automatically source your .bashrc. This should be enough to enable you to use conda within your script as if you were using it normally.

这篇关于Python - 通过 shell 脚本激活 conda env的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆