简体   繁体   English

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

[英]How can you “clone” a conda environment into the root environment?

I'd like the root environment of conda to copy all of the packages in another environment.我希望 conda 的根环境复制另一个环境中的所有包。 How can this be done?如何做到这一点?

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 .通常,在新环境中工作比更改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 root env for example:例如,要备份您的root

λ 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 (eg myenv ), export names + to a yaml file:在第二个环境(例如myenv )中, 将名称+ 导出到 yaml 文件:

λ activate myenv
λ conda env export > environment.yml  

then update the first environment + (eg root ) with the yaml file:然后使用 yaml 文件更新第一个环境+ (例如root ):

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

Option 2 - Cloning an environment选项 2 - 克隆环境

Use the --clone flag 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 env for more details on the env sub-commands. conda env有关 env 子命令的更多详细信息。
  • Anaconada Navigator desktop program for a more graphical experience. Anaconada Navigator桌面程序,提供更图形化的体验。
  • Docs on updated commands.有关更新命令的文档 With older conda versions use activate (Windows) and source activate (Linux/Mac OS).对于较旧的 conda 版本,请使用activate (Windows) 和source activate (Linux/Mac OS)。 Newer versions of conda can use conda activate (this may require some setup with your shell configuration via conda init ).较新版本的 conda 可以使用conda activate (这可能需要通过conda init对 shell 配置进行一些设置)。
  • Discussion on keeping conda env关于保持conda env讨论

Extras附加功能

There appears to be an undocumented conda run option to help execute commands in specific environments.似乎有一个未记录的conda run选项来帮助在特定环境中执行命令。

# New command
λ conda run --name myenv conda list --explicit > spec_file.txt

The latter command is effective at running commands in environments without the activation/deactivation steps.后一个命令在没有激活/停用步骤的环境中运行命令时有效。 See the equivalent command below:请参阅下面的等效命令:

# Equivalent
λ activate myenv
λ conda list --explicit > spec_file.txt
λ deactivate

Note, this is likely an experimental feature, so this may not be appropriate in production until official adoption into the public API.请注意,这可能是一个实验性功能,因此在正式采用公共 API 之前,这可能不适用于生产。

+ Conda docs have changed since the original post; +自原始帖子以来,Conda 文档已更改; 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;与前两个选项不同,spec-files 只捕获 conda 依赖项的链接; pip dependencies are not included.不包括 pip 依赖项。

To make a copy of your root environment (named base ), you can use following command;要复制您的根环境(名为base ),您可以使用以下命令; worked for me with Anaconda3-5.0.1:使用 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>

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

I also ran into the trouble of cloning an environment onto another machine and wanted to provide an answer.我也遇到了将环境克隆到另一台机器上的麻烦,想提供一个答案。 The key issue I had was addressing errors when the current environment contains development packages which cannot be obtained directly from conda install or pip install .当当前环境包含无法直接从conda installpip install获得的开发包时,我遇到的关键问题是解决错误。 For these cases I highly recommend conda-pack (see this answer ):对于这些情况,我强烈推荐conda-pack (请参阅此答案):

pip install conda-pack

or,或者,

conda install conda-pack

then back up the environment, to use the current environment just omit the my_env name,然后备份环境,使用当前环境只需省略 my_env 名称,

# Pack environment my_env into my_env.tar.gz
$ conda pack -n my_env

# Pack environment my_env into out_name.tar.gz
$ conda pack -n my_env -o out_name.tar.gz

# Pack environment located at an explicit path into my_env.tar.gz
$ conda pack -p /explicit/path/to/my_env

and restoring,和恢复,

# Unpack environment into directory `my_env`
$ mkdir -p my_env
$ tar -xzf my_env.tar.gz -C my_env

# Use Python without activating or fixing the prefixes. Most Python
# libraries will work fine, but things that require prefix cleanups
# will fail.
$ ./my_env/bin/python

# Activate the environment. This adds `my_env/bin` to your path
$ source my_env/bin/activate

# Run Python from in the environment
(my_env) $ python

# Cleanup prefixes from in the active environment.
# Note that this command can also be run without activating the environment
# as long as some version of Python is already installed on the machine.
(my_env) $ conda-unpack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM