Python 如何将 conda 环境“克隆”到根环境中?

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

How can you "clone" a conda environment into the root environment?

pythonanacondaconda

提问by mikal94305

I'd like the root environment of conda to copy all of the packages in another environment. How can this be done?

我希望 conda 的根环境复制另一个环境中的所有包。如何才能做到这一点?

回答by pylang

There are options to copy dependency names/urls/versions to files.

可以选择将依赖项名称/urls/versions 复制到文件。

Recommendation

推荐

Normally it is safer to work from a new environment rather than changing root. However, consider backing up your existing environments before attempting changes. Verify the desired outcome by testing these commands in a demo environment. To backup your rootenv for example:

通常,在新环境中工作比改变环境更安全root。但是,请考虑在尝试更改之前备份现有环境。通过在演示环境中测试这些命令来验证所需的结果。root例如,要备份您的env:

λ conda activate root
λ conda env export > environment_root.yml
λ conda list --explicit > spec_file_root.txt

Options

选项

Option 1- YAML file

选项 1- YAML 文件

Within the second environment (e.g. myenv), export names+to a yaml file:

在第二个环境中(例如myenv),将名称+导出到 yaml 文件:

λ activate myenv
λ conda env export > environment.yml  

then update the first environment+(e.g. root) with the yaml file:

然后使用 yaml 文件更新第一个环境+(例如root):

λ conda env update --name root --file environment.yml     


Option 2- Cloning an environment

选项 2- 克隆环境

Use the --cloneflag to clone environments (see @DevC's post):

使用该--clone标志来克隆环境(参见@DevC 的帖子):

λ conda create --name myclone --clone root

This basically creates a direct copy of an environment.

这基本上创建了环境的直接副本。



Option 3- Spec file

选项 3- 规范文件

Make a spec-file++to append dependencies from an env (see @Ormetrom):

制作规范文件++以从 env 附加依赖项(请参阅@Ormetrom):

λ activate myenv
λ conda list --explicit > spec_file.txt
λ conda install --name root --file spec_file.txt

Alternatively, replicate a new environment (recommended):

或者,复制一个新环境(推荐):

λ conda create --name myenv2 --file spec_file.txt

See Also

也可以看看

  • conda envfor more details on the env sub-commands.
  • Anaconada Navigatordesktop program for a more graphical experience.
  • Docson updated commands. With older conda versions use activate(Windows) and source activate(Linux/Mac OS).
  • Discussionon keeping conda env
  • conda env有关 env 子命令的更多详细信息。
  • Anaconada Navigator桌面程序,提供更图形化的体验。
  • 有关更新命令的文档。对于较旧的 conda 版本,请使用activate(Windows) 和source activate(Linux/Mac OS)。
  • 关于保留的讨论conda env

+Conda docs have changed since the original post; links updated.++Spec-files only work with environments created on the same OS. Unlike the first two options, spec-files only capture links to conda dependencies; pip dependencies are not included.

+自原始帖子以来,Conda 文档已更改;链接已更新。++规范文件仅适用于在同一操作系统上创建的环境。与前两个选项不同,spec-files 只捕获 conda 依赖项的链接;不包括 pip 依赖项。

回答by DevC

To make a copy of your root environment (named base), you can use following command; worked for me with Anaconda3-5.0.1:

要复制您的根环境(名为base),您可以使用以下命令;使用 Anaconda3-5.0.1 为我工作:

conda create --name <env_name> --clone base

you can list all the packages installed in conda environment with following command

您可以使用以下命令列出 conda 环境中安装的所有软件包

conda list -n <env_name>

回答by Ormetrom2354

When setting up a new environment and I need the packages from the base environment in my new one (which is often the case) I am building in the prompt a identical conda environment by using a spec-file.txt with:

在设置新环境并且我需要新环境中基本环境中的包时(通常是这种情况),我在提示中使用 spec-file.txt 构建了一个相同的 conda 环境:

conda list --explicit > spec-file.txt

The spec-file includes the packages of for example the base environment.

规范文件包括例如基础环境的包。

Then using the prompt I install the the packages into the new environment:

然后使用提示将软件包安装到新环境中:

conda install --name myenv --file spec-file.txt

The packages from base are then available in the new environment.

来自 base 的包随后可在新环境中使用。

The whole process is describe in the doc: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#building-identical-conda-environments

整个过程在文档中进行了描述:https: //docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#building-identical-conda-environments