Python 打包:pyproject.toml VS setup_requires 中的构建要求

Python packaging: build requirements in pyproject.toml VS setup_requires

在有些复杂的 Python setup.py 配置中,通常需要其他已经存在的库才能执行 setuptools.setup。在我的例子中,这将是 setuptools>=45.0cython>=0.29。现在,我有两个选项来声明这些构建时要求(不要与通常在 requirements.txt 文件中找到的标准包安装要求混淆)以便将此项目发送到 PyPI:

  1. setup_requires 参数中手动写入要求作为 setup.py 的一部分:
#setup.py
from setuptools import setup
#...
setup(
    name='bla',
    #...
    setup_requires = ['setuptools>=45.0', 'cython>=0.29'],
)
  1. 将这些要求写入一个单独的 pyproject.toml 文件,紧跟 PEP518:
#pyproject.toml
[build-system]
# Minimum requirements for the build system to execute.
requires = ["setuptools>=45.0", "cython>=0.29"]

它们可以互换吗?应该使用哪一个?为什么?

创建上述 PEP 是为了解决 Rationale section. Packaging Python 建议使用第二种方法中列出的第一种方法的局限性。