DEV Community

Cover image for 'Hypermodernize' your Python Package
Christian Ledermann
Christian Ledermann

Posted on

'Hypermodernize' your Python Package

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:

  1. 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.
  2. 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.
  3. Modern Tooling: Some modern Python tools rely on pyproject.toml for project configuration. Adopting pyproject.toml aligns with these tools and facilitates a smoother integration with them.
  4. Readability and Maintainability: The pyproject.toml format is often considered more readable and straightforward than the Python code used in setup.py. This can contribute to better maintainability, especially for more complex projects.
  5. Standardization: As Python packaging evolves, pyproject.toml is becoming the de facto standard for configuration, supported by tools like pip and build systems like flit and poetry.

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 to setup.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]
Enter fullscreen mode Exit fullscreen mode

or if you get an error no matches found: ini2toml[full]

pip install setuptools-py2cfg ini2toml configupdater tomlkit
Enter fullscreen mode Exit fullscreen mode

Create a temporary cfg file

setuptools-py2cfg > tmp.cfg
Enter fullscreen mode Exit fullscreen mode

and convert it into a toml file:

ini2toml --output-file=tmp.toml --profile=setup.cfg tmp.cfg
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
pyprojectsort
validate-pyproject pyproject.toml
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
ldrscke profile image
Christian Ledermann

pretty-toml-sort may be an alternative to pyprojectsort