如何在setuptools项目的pyproject.toml中引用requirements.txt?

blu*_*e10 63 python pip setuptools pyproject.toml

我正在尝试将基于setuptools的项目从传统配置迁移setup.py到现代pyproject.toml配置。

同时,我希望保持基于 的完善工作流程pip-compile,即 arequirements.in被编译为 a requirements.txt(当然对于最终用户/非库项目)。由于完全透明,这具有重要的好处:

  • 由于固定了依赖项的完全传递闭包,因此安装可实现 100% 可重现。
  • 更好地理解依赖传递闭包中的依赖冲突。

因此,我不想pyproject.toml通过dependencies = []列表直接在内部维护依赖项,而是在pip-compiled托管的外部维护requirements.txt

这让我想知道:有没有一种方法可以requirements.txtpyproject.toml配置中引用文件,而不必回退到setup.py脚本?

pig*_*mer 86

使用动态元数据

[project]
dynamic = ["dependencies"]
[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}
Run Code Online (Sandbox Code Playgroud)

请注意,引用的文件将使用类似requirements.txt-的语法;每行必须符合PEP 508 ,因此其中不支持、、 和 等标志。-r-c-erequirements.txt

此外:

如果您使用旧版本的setuptools,您可能需要确保指令引用的所有文件file都包含在(您可以通过或使用诸如 之类的插件来sdist做到这一点,请查看 [sic] Controlling files in the distribution了解更多信息)。MANIFEST.insetuptools-scm

版本 66.1.0 中的更改:较新版本的setuptools会自动将这些文件添加到sdist.

optional-dependencies如果您想将与 a一起使用requirements-dev.txt,则需要添加一个额外的组,如下所示(归功于 Billbottom):

[project]
dynamic = ["dependencies", "optional-dependencies"]
[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}
optional-dependencies = {dev = { file = ["requirements-dev.txt"] }}
Run Code Online (Sandbox Code Playgroud)