Specifying build requirements as a file in a setuptools pyproject.toml

171 views Asked by At

Setuptools supports dynamic metadata for project properties in pyproject.toml, and as a PEP517 backend, it also has the option to specify build requirements by implementing get_requires_for_build_wheel. But I cannot figure out whether it uses the chance and does implement a way to specify build requirements based on configuration options, and if so, how to specify it in the pyproject.toml.

I naively tried

[build-system]
requires = {file = "requirements-build.txt"}

but that understandably leads to pip complaining “This package has an invalid build-system.requires key in pyproject.toml. It is not a list of strings.” And adding

[project]
dynamic = ["build-system.requires"]

also doesn't work, because the possible options of dynamic are explicitly enumerated. I would be somewhat surprised if there wasn't an option for this, given that all the infrastructure elements are available, but how do I specify it?

2

There are 2 answers

0
Kitten Wizard Jr On

After doing some of my own research this is not possible using setuptools, as the build-system requires specification states that these are the requirements to run setuptools. Implying that it will fail rather than starting to process dynamic values.

setuptools pyproject.toml documentation shows that it only supports the following dynamic data keys.

Key Directive Notes
version attr,file
readme file Here you can also set "content-type":readme = {file =["README.txt", "USAGE.txt"], content-type = "text/plain"} If content-type is not given, "text/x-rst" is used by default.
description file One-line text (no line breaks)
classifiers file Multi-line text with one classifier per line
entry-points file INI format following Entry points specification (console_scripts and gui_scripts can be included)
dependencies file subset of the requirements.txt format (# comments and blank lines excluded) BETA optional-dependencies subset of the requirements.txt format per group (# comments and blank lines excluded) BETA

Additonally, PEP 518 specifies the following:

The [build-system] table is used to store build-related data. Initially only one key of the table will be valid and is mandatory for the table: requires. This key must have a value of a list of strings representing PEP 508 dependencies required to execute the build system (currently that means what dependencies are required to execute a setup.py file).

For the vast majority of Python projects that rely upon setuptools, the pyproject.toml file will be:

[build-system]
#Minimum requirements for the build system to execute.
requires = ["setuptools", "wheel"]  # PEP 508 specifications.

Because the use of setuptools and wheel are so expansive in the community at the moment, build tools are expected to use the example configuration file above as their default semantics when a pyproject.toml file is not present.

Tools should not require the existence of the [build-system] table. A pyproject.toml file may be used to store configuration details other than build-related data and thus lack a [build-system] table legitimately. If the file exists but is lacking the [build-system] table then the default values as specified above should be used. If the table is specified but is missing required fields then the tool should consider it an error.

0
sinoroc On

As far as I know, it is not possible.

If it is really necessary for your use case, and you think it is worth the cost, maybe it is possible to add some dynamic behavior here by (mis-)using the "in-tree build backends" feature.