Python Anaconda 导出环境文件

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

Anaconda export Environment file

pythonpython-3.xanacondaconda

提问by Lau

How can I make anaconda environment file which could be use on other computers?

如何制作可以在其他计算机上使用的anaconda环境文件?

I exported my anaconda python environment to YML using conda env export > environment.yml. The exported environment.ymlcontains this line prefix: /home/superdev/miniconda3/envs/juicyenvwhich maps to my anaconda's location which will be different on other's pcs.

我将 anaconda python 环境导出到 YML 使用conda env export > environment.yml. 导出的environment.yml包含这一行prefix: /home/superdev/miniconda3/envs/juicyenv,它映射到我的 anaconda 的位置,这在其他人的电脑上会有所不同。

回答by Andrew Guy

I can't find anything in the condaspecs which allow you to export an environment file without the prefix: ...line. However, as Alex pointed outin the comments, conda doesn't seem to care about the prefix line when creating an environment from file.

我在conda规范中找不到任何允许您在没有prefix: ...行的情况下导出环境文件的内容。但是,正如Alex在评论中指出的那样,从文件创建环境时,conda 似乎并不关心前缀行。

With that in mind, if you want the other user to have no knowledge of your default install path, you can remove the prefix line with grepbefore writing to environment.yml.

考虑到这一点,如果您希望其他用户不知道您的默认安装路径,您可以grep在写入environment.yml.

conda env export | grep -v "^prefix: " > environment.yml

Either way, the other user then runs:

无论哪种方式,另一个用户然后运行:

conda env create -f environment.yml

and the environment will get installed in their default conda environment path.

并且环境将安装在其默认的 conda 环境路径中。

If you want to specify a different install path than the default for your system (not related to 'prefix' in the environment.yml), just use the -pflag followed by the required path.

如果要为系统指定与默认安装路径不同的安装路径(与 environment.yml 中的“前缀”无关),只需使用-p后跟所需路径的标志即可。

conda env create -f environment.yml -p /home/user/anaconda3/envs/env_name

Note that Conda recommends creating the environment.ymlby hand, which is especially important if you are wanting to share your environment across platforms (Windows/Linux/Mac). In this case, you can just leave out the prefixline.

请注意,Conda 建议environment.yml手动创建,如果您想跨平台(Windows/Linux/Mac)共享您的环境,这一点尤其重要。在这种情况下,您可以省略该prefix行。

回答by javac

The easiest way to save the packages from an environment to be installed in another computer is:

从环境中保存要安装在另一台计算机上的软件包的最简单方法是:

$ conda list -e > req.txt

then you can install the environment using

然后你可以使用安装环境

$ conda create -n new environment --file req.txt

if you use pip, please use the following commands: reference https://pip.pypa.io/en/stable/reference/pip_freeze/

如果使用pip,请使用以下命令:参考https://pip.pypa.io/en/stable/reference/pip_freeze/

$ env1/bin/pip freeze > requirements.txt
$ env2/bin/pip install -r requirements.txt

回答by Ilyas

  • Linux

    conda env export --no-builds | grep -v "prefix" > environment.yml

  • Windows

    conda env export --no-builds | findstr -v "prefix" > environment.yml

  • Linux

    conda env export --no-builds | grep -v "前缀" > environment.yml

  • 视窗

    conda env export --no-builds | findstr -v "前缀" > environment.yml



Rationale: By default, conda env exportincludes the build information:

理由:默认情况下,conda env export包括构建信息:

$ conda env export
...
dependencies:
  - backcall=0.1.0=py37_0
  - blas=1.0=mkl
  - boto=2.49.0=py_0
...

You can instead export your environment without build info:

您可以在没有构建信息的情况下导出您的环境:

$ conda env export --no-builds
...
dependencies:
  - backcall=0.1.0
  - blas=1.0
  - boto=2.49.0
...

Which unties the environment from the Python version and OS.

这将环境与 Python 版本和操作系统结合起来。

回答by Nicole Finnie

I find exporting the packages in string format only is more portable than exporting the whole condaenvironment. As the previous answer already suggested:

我发现仅以字符串格式导出包比导出整个conda环境更具可移植性。正如之前的答案已经建议的那样:

$ conda list -e > requirements.txt

However, this requirements.txtcontains build numbers which are not portable between operating systems, e.g. between Macand Ubuntu. In conda env exportwe have the option --no-buildsbut not with conda list -e, so we can remove the build number by issuing the following command:

但是,这requirements.txt包含在操作系统之间不可移植的内部版本号,例如在Mac和之间Ubuntu。在conda env export我们有选项--no-builds但没有 with conda list -e,所以我们可以通过发出以下命令来删除内部版本号:

$ sed -i -E "s/^(.*\=.*)(\=.*)//" requirements.txt 

And recreate the environment on another computer:

并在另一台计算机上重新创建环境:

conda create -n recreated_env --file requirements.txt 

回答by M?ltus

  1. First activate your conda environment (the one u want to export/backup)
  1. 首先激活您的 conda 环境(您要导出/备份的环境)
conda activate myEnv
  1. Export all packages to a file (myEnvBkp.txt)
  1. 将所有包导出到一个文件 (myEnvBkp.txt)
conda list --explicit > myEnvBkp.txt
  1. Restore/import the environment:
  1. 恢复/导入环境:
conda create --name myEnvRestored --file myEnvBkp.txt