Migration from setup.py to pyproject.toml: how to specify package name?
Question:
I’m currently trying to move our internal projects away from setup.py
to pyproject.toml
(PEP-518). I’d like to not use build backend specific configuration if possible, even though I do specify the backend in the [build-system]
section by require
‘ing it.
The pyproject.toml
files are more or less straight-forward translations of the setup.py
files, with the metadata set according to PEP-621, including the dependencies
. We are using setuptools_scm
for the determination of the version, therefore the version
field ends up in the dynamic
section.
We used to set the packages
parameter to setup
in our setup.py
files, but I couldn’t find any corresponding field in pyproject.toml
, so I simply omitted it.
When building the project using python3 -m build .
, I end up with a package named UNKNOWN
, even though I have the name
field set in the [project]
section. It seems that this breaks very early in the build:
$ python -m build .
* Creating virtualenv isolated environment...
* Installing packages in isolated environment... (setuptools, setuptools_scm[toml]>=6.2, wheel)
* Getting dependencies for sdist...
running egg_info
writing UNKNOWN.egg-info/PKG-INFO
....
I’m using python 3.8.11 and the following packages:
build==0.8.0
distlib==0.3.4
filelock==3.4.1
packaging==21.3
pep517==0.12.0
pip==22.0.4
platformdirs==2.4.0
pyparsing==3.0.9
setuptools==62.1.0
six==1.16.0
tomli==1.2.3
virtualenv==20.14.1
wheel==0.37.1
My (abbreviated) pyproject.toml
looks like this:
[project]
name = "coolproject"
dependencies = [
'pyyaml==5.3',
'anytree==2.8.0',
'pytest'
]
dynamic = [
"version"
]
[build-system]
requires = ["setuptools", "wheel", "setuptools_scm[toml]>=6.2"]
[tool.setuptools_scm]
Any ideas?
Answers:
On Debian/Ubuntu "UNKNOWN" packages can be created if an older system version of setuptools is installed as well.
Workaround:
sudo apt purge python3-setuptools
Turning @AKX’s comments into an answer so that other people can find it more easily.
The problem may be an outdated pip/setuptools on the system. Apparently, version 19.3.1 which I have on my system cannot install a version of setuptools that can handle PEP621 metadata correctly.
You cannot require a new pip from within pyproject.toml using the build-system.requires
directive.
In case you cannot update the system pip, you can always install on a per-user basis:
pip install --user pip
and you’re good to go.
I’m currently trying to move our internal projects away from setup.py
to pyproject.toml
(PEP-518). I’d like to not use build backend specific configuration if possible, even though I do specify the backend in the [build-system]
section by require
‘ing it.
The pyproject.toml
files are more or less straight-forward translations of the setup.py
files, with the metadata set according to PEP-621, including the dependencies
. We are using setuptools_scm
for the determination of the version, therefore the version
field ends up in the dynamic
section.
We used to set the packages
parameter to setup
in our setup.py
files, but I couldn’t find any corresponding field in pyproject.toml
, so I simply omitted it.
When building the project using python3 -m build .
, I end up with a package named UNKNOWN
, even though I have the name
field set in the [project]
section. It seems that this breaks very early in the build:
$ python -m build .
* Creating virtualenv isolated environment...
* Installing packages in isolated environment... (setuptools, setuptools_scm[toml]>=6.2, wheel)
* Getting dependencies for sdist...
running egg_info
writing UNKNOWN.egg-info/PKG-INFO
....
I’m using python 3.8.11 and the following packages:
build==0.8.0
distlib==0.3.4
filelock==3.4.1
packaging==21.3
pep517==0.12.0
pip==22.0.4
platformdirs==2.4.0
pyparsing==3.0.9
setuptools==62.1.0
six==1.16.0
tomli==1.2.3
virtualenv==20.14.1
wheel==0.37.1
My (abbreviated) pyproject.toml
looks like this:
[project]
name = "coolproject"
dependencies = [
'pyyaml==5.3',
'anytree==2.8.0',
'pytest'
]
dynamic = [
"version"
]
[build-system]
requires = ["setuptools", "wheel", "setuptools_scm[toml]>=6.2"]
[tool.setuptools_scm]
Any ideas?
On Debian/Ubuntu "UNKNOWN" packages can be created if an older system version of setuptools is installed as well.
Workaround:
sudo apt purge python3-setuptools
Turning @AKX’s comments into an answer so that other people can find it more easily.
The problem may be an outdated pip/setuptools on the system. Apparently, version 19.3.1 which I have on my system cannot install a version of setuptools that can handle PEP621 metadata correctly.
You cannot require a new pip from within pyproject.toml using the build-system.requires
directive.
In case you cannot update the system pip, you can always install on a per-user basis:
pip install --user pip
and you’re good to go.