简体   繁体   English

在 pip 中拆分需求文件

[英]Split requirements files in pip

To create Python virtual environments I use virtualenv and pip.要创建 Python 虚拟环境,我使用 virtualenv 和 pip。 The workflow is very simple:工作流程非常简单:

$ virtualenv project
$ cd project
$ . bin/activate
$ pip install -r /path/to/requirements/req1.txt
$ pip install -r /path/to/requirements/req2.txt

The number of different requirement files can grow enough to make handy to have a way to include them at once, so I'd rather prefer to say:不同需求文件的数量可以增长到足以让我们有办法一次包含它们,所以我宁愿说:

$ pip install -r /path/to/requirements/req1_req2.txt

with req1_req2.txt containing something like: req1_req2.txt包含如下内容:

include /path/to/requirements/req1.txt
include /path/to/requirements/req2.txt

or otherwise:或其他:

$ pip install -r /path/to/requirements/*.txt

None of that works and however much simple it could be, I can't figure out how to do what I want.这些都不起作用,无论它多么简单,我都不知道如何做我想做的事。

Any suggestion?有什么建议吗?

The -r flag isn't restricted to command-line use only, it can also be used inside requirements files. -r标志不仅限于命令行使用,它也可以在需求文件中使用。 So running pip install -r req-1-and-2.txt when req-1-and-2.txt contains this:因此,当 req-1-and-2.txt 包含以下内容时运行pip install -r req-1-and-2.txt

-r req-1.txt
-r req-2.txt

will install everything specified in req-1.txt and req-2.txt.将安装 req-1.txt 和 req-2.txt 中指定的所有内容。

Just on a note, you can also split the requirements based on your groupings and embed them in a single file ( or again can prepare multiple requirements file based on your environment), that you can execute.请注意,您还可以根据您的分组拆分需求并将它们嵌入到一个文件中(或者再次可以根据您的环境准备多个需求文件),您可以执行。

For example, the test requirements here:比如这里的测试要求:

requirements-test.txt需求测试.txt

pylint==2.4.4
pytest==5.3.2

The dev requirements here:这里的开发要求:

requirements-dev.txt需求-dev.txt

boto3>=1.12.11

Master requirements file containing your other requirements:包含您的其他要求的主要求文件:

requirements.txt要求.txt

-r requirements-dev.txt
-r requirements-test.txt

Now, you can just install the requirements file embedding your other requirements现在,您只需安装嵌入其他要求的要求文件

pip3 install -r requirements.txt

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

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