康达:激活环境后会发生什么? [英] Conda: what happens when you activate an environment?

查看:75
本文介绍了康达:激活环境后会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行source activate <env-name>如何更新$ PATH变量?我一直在查看CONDA-INSTALLATION/bin/activate脚本,但不了解conda如何更新$ PATH变量以包括最近激活的环境的bin目录.没有我可以在哪里找到conda用来在$ PATH变量前添加代码的地方.

How does running source activate <env-name> update the $PATH variable? I've been looking in the CONDA-INSTALLATION/bin/activate script and do not understand how conda updates my $PATH variable to include the bin directory for the recently activated environment. No where can I find the code that conda uses to prepend my $PATH variable.

推荐答案

免责声明:我不是conda开发人员,也不是Bash专家.下面的解释是基于我对代码的跟踪,希望我一切都好.另外,下面的所有链接都是在编写此答案时指向主提交的永久链接( ).行为/行为在将来的提交中可能会更改.当心:前方的兔子洞很深!

Disclaimer: I am not a conda developer, and I'm not a Bash expert. The following explanation is based on me tracing through the code, and I hope I got it all right. Also, all of the links below are permalinks to the master commit at the time of writing this answer (7cb5f66). Behavior/lines may change in future commits. Beware: Deep rabbit hole ahead!

请注意,此说明是针对命令source activate env-name的,但是在conda> = 4.4中,建议的激活环境的方法是conda activate env-name.我认为,如果使用conda activate env-name,则应该在进入cli.main函数的那部分进行解释.

Note that this explanation is for the command source activate env-name, but in conda>=4.4, the recommended way to activate an environment is conda activate env-name. I think if one uses conda activate env-name, you should pick up the explanation around the part where we get into the cli.main function.

对于conda> = 4.4,<4.5,查看CONDA_INST_DIR/bin/activate,我们发现倒数第二行(

For conda >=4.4,<4.5, looking at CONDA_INST_DIR/bin/activate, we find on the second to last and last lines (GitHub link):

. "$_CONDA_ROOT/etc/profile.d/conda.sh" || return $?
_conda_activate "$@"

第一行在$_CONDA_ROOT/etc/profile.d目录中提供脚本conda.sh,该脚本定义_conda_activate bash函数,我们将参数$@传递给该函数,该参数基本上是我们传递给的所有参数. activate脚本.

The first line sources the script conda.sh in the $_CONDA_ROOT/etc/profile.d directory, and that script defins the _conda_activate bash function, to which we pass the arguments $@ which is basically all of the arguments that we passed to the activate script.

沿着兔子洞的下一步,我们查看$_CONDA_ROOT/etc/profile.d/conda.sh并找到(

Taking the next step down the rabbit hole, we look at $_CONDA_ROOT/etc/profile.d/conda.sh and find (GitHub link):

_conda_activate() {
# Some code removed...
    local ask_conda
    ask_conda="$(PS1="$PS1" $_CONDA_EXE shell.posix activate "$@")" || return $?
    eval "$ask_conda"

    _conda_hashr
}

关键是该行ask_conda=...,尤其是$_CONDA_EXE shell.posix activate "$@".在这里,我们使用参数shell.posixactivate以及随后传递给该函数的其余参数(即我们要激活的环境名称)运行conda可执行文件.

The key is that line ask_conda=..., and particularly $_CONDA_EXE shell.posix activate "$@". Here, we are running the conda executable with the arguments shell.posix, activate, and then the rest of the arguments that got passed to this function (i.e., the environment name that we want to activate).

另一步进入兔子洞...从这里,conda可执行文件调用运行execute方法.

Another step into the rabbit hole... From here, the conda executable calls the cli.main function and since the first argument starts with shell., it imports the main function from conda.activate. This function creates an instance of the Activator class (defined in the same file) and runs the execute method.

execute方法处理参数,并存储传递的环境名称放入实例变量,然后确定 activate命令已通过,因此它运行activate方法.

The execute method processes the arguments and stores the passed environment name into an instance variable, then decides that the activate command has been passed, so it runs the activate method.

进入兔子洞的另一步... activate方法调用 build_activate方法,它调用 build_activate方法将prefix添加到PATH 通过 _add_prefix_to_path方法.最后,build_activate方法返回一个字典需要运行以激活"环境的命令.

Another step into the rabbit hole... The activate method calls the build_activate method, which calls another function to process the environment name to find the environment prefix (i.e., which folder the environment is in). Finally, the build_activate method adds the prefix to the PATH via the _add_prefix_to_path method. Finally, the build_activate method returns a dictionary of commands that need to be run to "activate" the environment.

再往前走一步... build_activate方法返回的字典由 _finalize方法返回的值返回临时文件的名称.临时文件具有设置所有适当的环境变量所需的命令.

And another step deeper... The dictionary returned from the build_activate method gets processed into shell commands by the _yield_commands method, which are passed into the _finalize method. The activate method returns the value from running the _finalize method which returns the name of a temp file. The temp file has the commands required to set all of the appropriate environment variables.

现在,退一步,在activate.main函数中,execute方法的返回值(即临时文件的名称)为

Now, stepping back out, in the activate.main function, the return value of the execute method (i.e., the name of the temp file) is printed to stdout. This temp file name gets stored in the Bash variable ask_conda back in the _conda_activate Bash function, and finally, the temp file is executed by the eval Bash function.

Ph!我希望我一切都好.正如我所说,我不是conda开发人员,并且与Bash专家相距甚远,所以请原谅我使用的所有解释性捷径都不是100%正确的.只需发表评论,我将很乐意修复它!

Phew! I hope I got everything right. As I said, I'm not a conda developer, and far from a Bash expert, so please excuse any explanation shortcuts I took that aren't 100% correct. Just leave a comment, I'll be happy to fix it!

我还应该注意,建议在conda> = 4.4中激活环境的推荐方法是conda activate env-name,这是如此令人费解的原因之一-激活现在主要在Python中处理,而(我认为)以前它或多或少是直接在Bash/CMD中处理的.

I should also note that the recommended method to activate environments in conda >=4.4 is conda activate env-name, which is one of the reasons this is so convoluted - the activation is mostly handled in Python now, whereas (I think) previously it was more-or-less handled directly in Bash/CMD.

这篇关于康达:激活环境后会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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