Christian Ledermann
Posted on December 7, 2023
In the original Hypermodern Python Blogpost, Poetry was recommended as the preferred tool.
There are quite a lot of packaging tools out there which I do not want to go into in depth, instead I recommend Anna-Lena Popkes An unbiased evaluation of environment management and packaging tools.
What has emerged as the preferred way to store packaging information is the pyproject.toml
file.
Using pyproject.toml
over setup.py
has become a preferred choice for several reasons:
-
Consistent Configuration:
pyproject.toml
is part of the PEP 518 and PEP 517 specifications, offering a standardized and consistent way to declare build configurations and dependencies. -
PEP 518 Support:
pyproject.toml
supports PEP 518, allowing the declaration of build system requirements, enabling better compatibility with modern build tools and providing more flexibility in the build process. -
Modern Tooling: Some modern Python tools rely on
pyproject.toml
for project configuration. Adoptingpyproject.toml
aligns with these tools and facilitates a smoother integration with them. -
Readability and Maintainability: The
pyproject.toml
format is often considered more readable and straightforward than the Python code used insetup.py
. This can contribute to better maintainability, especially for more complex projects. -
Standardization: As Python packaging evolves,
pyproject.toml
is becoming the de facto standard for configuration, supported by tools likepip
and build systems likeflit
andpoetry
.
The adoption of pyproject.toml
aligns with modern best practices in the Python packaging ecosystem.
Converting a setup.py
base package to a pyproject.toml
based one turns out to be straightforward for most use cases.
While it is easy to write a pyproject.toml
there are a number of tools to convert legacy package information into this format.
-
setuptools-py2cfg A script for converting
setup.py
tosetup.cfg
- ini2toml which automatically translates .ini/.cfg files into TOML
-
validate-pyproject for automated checks on
pyproject.toml
powered by JSON Schema definitions - pyprojectsort enforces consistent formatting of pyproject.toml files, reducing merge request conflicts and saving time otherwise spent on manual formatting.
- pyroma Rate your Python packages package friendliness.
Example
Install the required packages
pip install setuptools-py2cfg ini2toml[full]
or if you get an error no matches found: ini2toml[full]
pip install setuptools-py2cfg ini2toml configupdater tomlkit
Create a temporary cfg
file
setuptools-py2cfg > tmp.cfg
and convert it into a toml
file:
ini2toml --output-file=tmp.toml --profile=setup.cfg tmp.cfg
Apart from the profile setup.cfg
ini2toml
also has profiles to convert settings to their pyproject.toml
equivalent for
.coveragerc
.isort.cfg
mypy.ini
-
pytest.ini
('ini_options' table)
After you converted the files, cut and paste them into a new (or existing) pyproject.toml
file and validate and format them.
pip install validate-pyproject pyprojectsort pyroma
pyprojectsort
validate-pyproject pyproject.toml
Now you can delete your old setup.py file and verify that your package still builds as expected with
rm setup.py
python -m build
pyroma .
Check the dist/[my-package-name].tar.gz
file to ensure it builds like before.
Adjust and tweak the settings as outlined in the user guide
Additional information
Posted on December 7, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.