Python 如何跨平台共享 conda 环境

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

How to share conda environments across platforms

pythoncross-platformconda

提问by user3731622

The conda docs at http://conda.pydata.org/docs/using/envs.htmlexplain how to share environments with other people.

http://conda.pydata.org/docs/using/envs.html 上的 conda 文档解释了如何与其他人共享环境。

However, the docs tell us this is not cross platform:

但是,文档告诉我们这不是跨平台的:

NOTE: These explicit spec files are not usually cross platform, and      
therefore have a comment at the top such as # platform: osx-64 showing the  
platform where they were created. This platform is the one where this spec
file is known to work. On other platforms, the packages specified might not
be available or dependencies might be missing for some of the key packages
already in the spec.

NOTE: Conda does not check architecture or dependencies when installing 
from an explicit specification file. To ensure the packages work correctly,
be sure that the file was created from a working environment and that it is 
used on the same architecture, operating system and platform, such as linux-
64 or osx-64.

Is there a good method to share and recreate a conda environment in one platform (e.g. CentOS) in another platform (e.g. Windows)?

是否有一种好方法可以在另一个平台(例如 Windows)中的一个平台(例如 CentOS)中共享和重新创建 conda 环境?

回答by Eric Dill

Answer

回答

This answer is given with the assumption that you would like to make sure that the same versions of the packages that you generally care about are on different platforms and that you don't care about the exact same versions of allpackages in the entire dependency tree. If you are trying to install the exact same version of all packages in your entire dependency tree that has a high likelihood of failure since some conda packages have different dependencies for osx/win/linux. For example, the recipe for otroboptwill install different packages on win vs. osx/linux, so the environment list would be different.

给出此答案的假设是,您希望确保您通常关心的相同版本的软件包位于不同的平台上,并且您不关心整个依赖关系树中所有软件包的完全相同的版本 . 如果您尝试在整个依赖关系树中安装完全相同版本的所有软件包,这很可能会失败,因为某些 conda 软件包对 osx/win/linux 具有不同的依赖关系。比如otrobopt的recipe会在win和osx/linux上安装不同的包,所以环境列表会不一样。

Recommendation: manually create an environment.yaml file and specify or pin only the dependencies that you care about.Let the conda solver do the rest. Probably worth noting is that conda-env(the tool that you use to manage conda environments) explicitly recommends that you "Always create your environment.yml file by hand."

建议:手动创建 environment.yaml 文件并仅指定或固定您关心的依赖项。让 conda 求解器完成剩下的工作。可能值得注意的是conda-env(您用来管理 conda 环境的工具)明确建议您“始终手动创建您的 environment.yml 文件”。

Then you would just do conda env create --file environment.yml

那你就做 conda env create --file environment.yml

Have a look at the readme for conda-env.

查看conda-env的自述文件 。

They can be quite simple:

它们可以很简单:

name: basic_analysis
dependencies:
  - numpy
  - pandas

Or more complex where you pin dependencies and specify anaconda.org channels to install from:

或者更复杂的固定依赖项并指定要从以下位置安装的 anaconda.org 频道:

name: stats-web
channels:
  - javascript
dependencies:
  - python=3.4   # or 2.7 if you are feeling nostalgic
  - bokeh=0.9.2
  - numpy=1.9.*
  - nodejs=0.10.*
  - flask
  - pip:
    - Flask-Testing

In either case, you can create an environment with conda env create --file environment.yaml

在任何一种情况下,您都可以创建一个环境 conda env create --file environment.yaml

If you have a more complex use case or further questions, update the original question and I'll see if I can help you a bit more.

如果您有更复杂的用例或其他问题,请更新原始问题,我会看看是否可以为您提供更多帮助。

回答by Adam Murphy

Whilst it is possible to create your environment.ymlfile by hand, you can ensure that your environment works across platforms by using the conda env export --from-historyflag.

虽然可以environment.yml手动创建文件,但您可以使用该conda env export --from-history标志确保您的环境跨平台工作。

This will only include packages that you've explicitly asked for, as opposed to including every package in your environment.

这将只包括您明确要求的包,而不是包括您环境中的每个包。

For example, if you create an environment and install a package conda install python=3.8 numpy, it will install numerous other dependencies as well as python and bumpy.

例如,如果您创建一个环境并安装一个包conda install python=3.8 numpy,它将安装许多其他依赖项以及 python 和颠簸。

If you then run conda env export > environment.yml, your environment.ymlfile will include all the additional dependencies conda automatically installed for you.

如果您然后运行conda env export > environment.yml,您的environment.yml文件将包含为您自动安装的所有附加依赖项 conda。

On the other hand, running conda env export --from-historywill just create environment.ymlwith python=3.8and numpyand thus will work across platforms.

在另一方面,运行conda env export --from-history只会创建environment.ymlpython=3.8numpy,因此将跨平台工作。

Answer adapted from the docs.

改编自docs 的答案。

回答by Irurik Soft Labs

conda-env export should be used used to export your complete environment to file named my_env.yml.

应使用 conda-env export 将完整环境导出到名为 my_env.yml 的文件。

Check the working solution on getting only prefix on OS X instead of complete dependency including pip.

检查在 OS X 上仅获取前缀而不是包括 pip 在内的完整依赖项的工作解决方案。

Step 1: deactivate from the environment if activated. else it will create yml file with only prefix.

第 1 步:如果已激活,则从环境中停用。否则它将创建只有前缀的 yml 文件。

Step 2: run below command to export conda-env export -n my_env > my_env.yml it will export every required dependency, channel and pip install in a yml file which is importable to share with others.

第 2 步:运行以下命令导出 conda-env export -n my_env > my_env.yml 它将导出所有必需的依赖项、通道和 pip 安装到 yml 文件中,该文件可导入与他人共享。

Step 3: run below command to import conda-env create -n my_env -f= my_env.yml it will create the exact environment as is on sharing fellow machine.

第 3 步:运行以下命令导入 conda-env create -n my_env -f= my_env.yml 它将创建与共享其他机器相同的环境。

回答by Allan

For those interested in a solution to maintain a single environment file that can be used in Linux, macOS, and Windows, please check the conda-devenvtool at https://github.com/ESSS/conda-devenv.

对于那些对维护可在 Linux、macOS 和 Windows 中使用的单个环境文件的解决方案感兴趣的人,请查看https://github.com/ESSS/conda-devenv 上conda-devenv工具。