Pip supports the pyproject.toml
file but so far all practical usage of the new schema requires a 3rd party tool that auto-generates these files (e.g., poetry and pip). Unlike setup.py
which is already human-writeable, pyproject.toml
is not (yet).
From setuptools docs,
[build-system]
requires = [
"setuptools >= 40.9.0",
"wheel",
]
build-backend = "setuptools.build_meta"
However, this file does not include package dependencies (as outlined in PEP 621). Pip does support installing packages using pyproject.toml
but nowhere does pep specify how to write package dependencies in pyproject.toml
for the official build system setuptools
.
How do I write package dependencies in pyproject.toml
?
Related StackOverflow Questions:
How to init the pyproject.toml file
This question asks for a method to auto-generate
pyproject.toml
, my question differ because I ask for a human-writtenpyproject.toml
.
First, the
pyproject.toml
file is always "human-writable", it was designed to be written by humans first.Then, it is important to know that in this context setuptools and Poetry take the role of what are called "build back-ends", and there are many such build back-ends available today, setuptools and Poetry (technically
poetry-core
) are just two examples of them.As of today, it seems like most (if not all) of the build back-ends I know of expect their configuration (including dependencies) to be written in
pyproject.toml
.[project]
There is a standard specifying how a project's packaging metadata, including dependencies, should be laid out in the
pyproject.toml
file under a[project]
table: "Declaring project metadata: the[project]
table".Here is a list of build back-ends I know of that follow this
[project]
standard:enscons
flit_core
(seeflit
)hatchling
(seehatch
)maturin
meson-python
pdm-backend
(seepdm
)scikit-build-core
setuptools
(experimental support since version61.0.0
)trampolim
whey
I have a comparison table here.
For all
[project]
-compatible build back-ends, the dependencies should be written in thepyproject.toml
file at the root of the project's source code directory like in the following example:References:
setuptools (before version
61.0.0
)In setuptools before version
61.0.0
there is no support for writing the project's packaging metadata inpyproject.toml
. You have to either write asetup.cfg
, or asetup.py
, or a combination of both.My recommendation is to write as much as possible in
setup.cfg
. Such asetup.cfg
could look like this:and in most cases the
setup.py
can be omitted completely or it can be as short as:References about the dependencies specifically:
Again, note that in most cases it is possible to omit the
setup.py
file entirely, one of the conditions is that thesetup.cfg
file and apyproject.toml
file are present and contain all the necessary information. Here is an example ofpyproject.toml
that works well for a setuptools build back-end:Poetry
In Poetry everything is defined in
pyproject.toml
, but it uses Poetry-specific sections[tool.poetry]
instead of the standardized[project]
section. There are some plans to add support for this standard in Poetry in the future.This file can be hand-written. As far as I can tell, there is no strict need to ever explicitly install poetry itself (commands such as
pip install
andpip wheel
can get you far enough).The
pyproject.toml
file can be as simple as:References:
pyproject.toml
file"