简体   繁体   English

Conda将一个环境导入到另一个环境

[英]Conda importing one environment.yml into another

Consider the situation where you're importing a Python submodule with dependencies into a project with its own dependencies. 考虑一下将具有依赖项的Python子模块导入具有自己依赖项的项目的情况。 Say that the submodule has its environment.yml file and the project itself has an environment.yml file. 假设子模块有其environment.yml文件,而项目本身有一个environment.yml文件。

Is there a way to create an environment incorporating both of these environment specifications, and if so, how would you go about doing this? 有没有办法创建一个同时包含这两个环境规范的环境,如果是,您将如何进行呢? (Or is there a better/more preferred way to handle this situation?) (或者是否有更好/更优选的方式来处理这种情况?)

I'm guessing you're the one developing both the submodule and the project since the dependencies are still in the environment.yml files. 我猜您是同时开发子模块和项目的人,因为依赖项仍在environment.yml文件中。

Option 1: Update Project Environment using Submodule's environment.yml 选项1:使用子模块的environment.yml更新项目环境

This unfortunately might be your only option if your submodule has dependencies on conda packages. 不幸的是,如果您的子模块依赖于conda软件包,那么这可能是您唯一的选择。

# First create the project environment
$ conda env create --force -f project_environment.yml

# Then update with submodule dependencies
$ conda env update -n project-env-name --file submodule_environment.yml

This is less than ideal since the basic expectation is that your imported libraries come with their own dependencies. 这不是理想的,因为基本的期望是您导入的库带有它们自己的依赖项。

Option 2: Put dependencies into respective requirements.txt files 选项2:将依赖项放入相应的requirements.txt文件中

This is applicable only if the submodule dependencies can be installed from PyPi via pip . 仅当可以通过pip从PyPi安装子模块依赖项时才适用。 First put the dependencies of the project and submodule into their respective requirements.txt files. 首先,将项目和子模块的依赖项放入其各自的requirements.txt文件中。

Then restructure the environment.yml files to look the following: 然后重组environment.yml文件,使其看起来如下所示:

submodule_environment.yml

name: submodule-env-name
channels:
  - defaults
dependencies:
- python=3.6.3             # no conda dependencies
- pip:
    - -r requirements.txt  # <--- submodule dependencies

project_environment.yml

name: project-env-name
channels:
  - defaults
dependencies:
- python=3.6.3
- pip:
    - -r requirements.txt                    # <--- project dependencies
    - -r project/submodule/requirements.txt  # <--- submodule dependencies

In this way you can ignore the submodule_environment.yml file altogether and then create the project environment with a single command. 这样,您可以完全忽略submodule_environment.yml文件,然后使用单个命令创建项目环境。

$ conda env create --force -f project_environment.yml

This approach will not work if your submodule has dependencies on conda packages. 如果您的submodule依赖于conda软件包,则此方法将不起作用。 If it does then Option 1 is your best option. 如果是这样,那么选项1是您的最佳选择。

Option 3: Package the Submodule (Ideal) 选项3:打包子模块(理想)

Assuming the submodule has no conda dependencies then it would be ideal to just make a separate package out of the submodule. 假设子模块没有conda依赖项,那么理想的是仅从子模块中制作一个单独的程序包。 Create a setup.py and put all the dependencies into the install_requires field. 创建一个setup.py并将所有依赖项放入install_requires字段中。 Here's a template of how the setup.py file should look like. 这是setup.py文件的外观模板。

Once it's packaged, you can do the following: 打包后,您可以执行以下操作:

  • Install it locally using: 使用以下命令在本地安装:
    • pip install .
  • Upload to github or bitbucket and install it using: 上载到github或bitbucket并使用以下方式安装:
    • pip install git+https://github.com/username/submodule.git --upgrade
  • Upload to github or bitbucket and add the following to requirements.txt or environment.yml under pip : 上传到github或bitbucket并将以下内容添加到pip下的requirements.txtenvironment.yml
    • git+https://github.com/username/submodule.git#egg=submodule

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

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