[英]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
文件中。
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. 这不是理想的,因为基本的期望是您导入的库带有它们自己的依赖项。
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是您的最佳选择。
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: 打包后,您可以执行以下操作:
pip install .
pip install git+https://github.com/username/submodule.git --upgrade
requirements.txt
or environment.yml
under pip
: pip
下的requirements.txt
或environment.yml
:
git+https://github.com/username/submodule.git#egg=submodule
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.