NLeSC / python-template

Netherlands eScience Center Python Template
https://research-software-directory.org/software/nlesc-python-template
Apache License 2.0
164 stars 74 forks source link

use `python -m build` to build #318

Closed egpbos closed 10 months ago

egpbos commented 2 years ago

Currently, the developer readme suggests to use python3 setup.py sdist bdist_wheel to build packages, in a freshly created virtual environment too. The official Python packaging docs recommend using python -m build. I think we should switch to this, also for consistency because explicit use of setup.py is no longer recommended for installing the package either. Mainly though because it is a lot simpler; it creates the virtual environment itself and so makes the instructions a lot shorter.

This requires:

  1. Updating the README.dev.md instructions: replace almost all of the code in the first code block in subsection "2/3" with just python -m build (keep twine as well).
  2. Adding the build package to the dev extras section in setup.cfg.

While we're at it, the instructions should also note that an account on PyPI and on TestPyPI (two separate accounts) are necessary for using twine.

egpbos commented 2 years ago

As part of this, we should in fact remove setup.py altogether, since its use has been deprecated. See for instance https://packaging.python.org/en/latest/tutorials/packaging-projects/#configuring-metadata:

Static metadata (setup.cfg) should be preferred. Dynamic metadata (setup.py) should be used only as an escape hatch when absolutely necessary. setup.py used to be required, but can be omitted with newer versions of setuptools and pip.

And https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html

egpbos commented 2 years ago

We should make sure that everything still works though. @bouweandela mentioned that possibly pip install -e . wouldn't work anymore and there were indeed discussions about this (https://discuss.python.org/t/pip-19-1-and-installing-in-editable-mode-with-pyproject-toml/1553/65). I tried it myself, and it doesn't seem to be a problem in my relatively fresh project, but there may be other things. For instance, I see that tox doesn't immediately work without setup.py, it mentions that it needs additional configuration when I remove setup.py:

ERROR: pyproject.toml file found.
To use a PEP 517 build-backend you are required to configure tox to use an isolated_build:
https://tox.readthedocs.io/en/latest/example/package.html
egpbos commented 2 years ago

The fix for tox is to just add isolated_build = True to the tox configuration section in pyproject.toml.