繁体   English   中英

Python3:是否应该在 requirements.txt 中包含项目中不一定使用的依赖项?

[英]Python3: Should you include dependencies that you don't necessarily use in the project in the requirements.txt?

在 Python 中,假设我正在使用像flake8这样的库来为我的项目执行样式指南,并且我还告诉我在同一项目上工作的编码人员,我们将使用flake8指南作为样式指南。 但是,我没有直接在我的项目中使用它。 我在命令行中运行 flake8 以检查我的项目是否遵循 flake8 准则,但不要在我的项目中将其用于任何其他目的。 如果是这种情况, flake8是属于requirements.txt文件的东西吗? 还是可以省略,只需要项目中实际使用的依赖, requirements.txt文件里面的go?

需求文件应仅包含程序的必需依赖项。 由于flake8是确保样式一致性的支持模块,因此不属于此类。 我不会包括在内。

Flake8 是开发依赖项。 意思是,虽然这些包不需要在生产环境中,但它们无论如何都应该在开发环境中。

按照这个 SO answer ,我建议使用多环境需求 txt 文件结构:

`-- your_project_root
|-- requirements
|   |-- common.txt
|   |-- dev.txt
|   `-- prod.txt
`-- requirements.txt

文件的内容如下所示:

通用.txt:

# Contains requirements common to all environments
req1~=1.0
req2~=1.0
req3~=1.0
...

开发.txt:

# Specifies only dev-specific requirements
# But imports the common ones too
-r common.txt
flake8~=6.0.0
dev_req~=1.0
...

产品.txt:

# Same for prod...
-r common.txt
prod_req~=1.0
...

然后你可以运行

pip install -r requirements/dev.txt

要么

pip install -r requirements/prod.txt

取决于你所处的环境。

根据您使用的其他构建工具和 CI/CD,您可能仍然需要一个requirements.txt ,在这种情况下您可以用您需要的内容填充它:

要求.txt:

# Mirrors prod
-r requirements/prod.txt

暂无
暂无

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

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