Search code examples
pythonpython-3.xsetuptoolspypipyproject.toml

Python packaging: build requirements in pyproject.toml VS setup_requires


In somewhat complex Python setup.py configurations, one typically needs other libraries already present in order to execute setuptools.setup. In my case, this would be setuptools>=45.0 and cython>=0.29. Now, I have two options to declare these build-time requirements (not to confuse with standard package installation requirements typically found in a requirements.txt file) in order to ship this project to PyPI:

  1. Manually write the requirements as part of setup.py in the setup_requires argument:
#setup.py
from setuptools import setup
#...
setup(
    name='bla',
    #...
    setup_requires = ['setuptools>=45.0', 'cython>=0.29'],
)
  1. Write these requirements into a separate pyproject.toml file following PEP518:
#pyproject.toml
[build-system]
# Minimum requirements for the build system to execute.
requires = ["setuptools>=45.0", "cython>=0.29"]

Are they exchangeable? Which one should be used and why?


Solution

  • The aforementioned PEP was created to address the limitations of the 1st approach that are listed in the Rationale section. Packaging Python recommends using the 2nd approach.