Python 如何使用 .yml 文件更新现有的 Conda 环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42352841/
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-08-19 21:36:17  来源:igfitidea点击:

How to update an existing Conda environment with a .yml file

pythondjangoanacondaconda

提问by tilikoom

How can a pre-existing conda environment be updated with another .yml file. This is extremely helpful when working on projects that have multiple requirement files, i.e. base.yml, local.yml, production.yml, etc.

如何使用另一个 .yml 文件更新预先存在的 conda 环境。这在处理具有多个需求文件(即base.yml, local.yml, production.yml等)的项目时非常有用。

For example, below is a base.ymlfile has conda-forge, conda, and pip packages:

例如,下面是一个base.yml包含 conda-forge、conda 和 pip 包的文件:

base.yml

基本文件

name: myenv
channels:
  - conda-forge
dependencies:
  - django=1.10.5
  - pip:
    - django-crispy-forms==1.6.1

The actual environment is created with: conda env create -f base.yml.

实际环境是通过以下方式创建的: conda env create -f base.yml.

Later on, additional packages need to be added to base.yml. Another file, say local.yml, needs to import those updates.

稍后,需要将其他包添加到base.yml. 另一个文件,例如local.yml,需要导入这些更新。

Previous attempts to accomplish this include:

以前实现这一目标的尝试包括:

creating a local.ymlfile with an import definition:

local.yml使用导入定义创建文件:

channels:

dependencies:
  - pip:
    - boto3==1.4.4
imports:
  - requirements/base. 

And then run the command: conda install -f local.yml.

然后运行命令: conda install -f local.yml.

This does not work. Any thoughts?

这不起作用。有什么想法吗?

回答by alkamid

Try using conda env update:

尝试使用conda env update

conda activate myenv
conda env update --file local.yml

Or without the need to activate the environment (thanks @NumesSanguis):

或者不需要激活环境(感谢@NumesSanguis):

conda env update --name myenv --file local.yml

回答by Blink

The suggested answer is partially correct. You'll need to add the --pruneoption to also uninstall packages that were removed from the environment.yml. Correct command:

建议的答案部分正确。您还需要添加--prune选项以卸载从 environment.yml 中删除的软件包。正确的命令:

conda env update -f local.yml --prune

回答by Dave

alkamid's answer is on the right lines, but I have found that Conda fails to install new dependencies if the environment is already active. Deactivating the environment first resolves this:

alkamid 的回答是正确的,但我发现如果环境已经处于活动状态,Conda 将无法安装新的依赖项。停用环境首先解决这个问题:

source deactivate;
conda env update -f whatever.yml;
source activate my_environment_name; # Must be AFTER the conda env update line!