setuptools not getting dynamic version when using pyproject.toml
Asked Answered
H

1

6

I am using setuptools with a pyproject.toml file, and want setuptools to get the package version dynamically from the package contents. Instead, it is always setting the package version in the name of the generated file to 0.0.0, even though the package version inside the package seems correct. What am I doing wrong?

  • Python 3.11.6 on MacOS 14.1.2 (Sonoma)
  • setuptools version 68.2.2
  • pip version 23.3.1

Package structure:

.
├── LICENSE.md
├── README.md
├── invperc
│   └── __init__.py
└── pyproject.toml
  • invperc/__init__.py contains only this:
__version__ = "0.2.0"
  • pyproject.toml contains only this:
[project]
name = "invperc"
description = "Invasion Percolation"
readme = "README.md"
authors = [
    { name = "Greg Wilson", email = "[email protected]" }
]
license = { text = "MIT License" }
dependencies = ["pandas", "numpy"]
dynamic = ["version"]

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tools.setuptools.dynamic]
version = {attr = "invperc.__version__"}
  • Command:
python -m build
  • Screen output:
...many lines...
Successfully built invperc-0.0.0.tar.gz and invperc-0.0.0-py3-none-any.whl
  • dist/invperc-0.0.0.tar.gz and dist/invperc-0.0.0-py3-none-any-whl now exist with 0.0.0 as version numbers (which is incorrect).

  • But if I import and check:

$ cd /tmp
$ pip install $HOME/invperc/dist/invperc-0.0.0-py3-none-any.whl
$ python
>>> import invperc
>>> invperc.__version__
'0.2.0'
Handyman answered 7/12, 2023 at 14:56 Comment(0)
H
8

Thanks to @[email protected] for the answer: [tools.setuptools.dynamic] should be [tool.setuptools.dynamic] with a singular "tool" instead of a plural "tools". Now excuse me while I go scream into a pillow about the lack of any kind of warning message...

Handyman answered 7/12, 2023 at 18:39 Comment(3)
Maybe there is a linter out there that could have helped. Maybe this one (untested): docs.astral.sh/ruff/rules/invalid-pyproject-tomlCilicia
You are not the first :) pypi.org/project/UNKNOWN/#history As usual, distutils is to blame.Hod
If you would like to prepare a PR after screaming into the pillow, it would be pretty easy to add a warning into setuptools' wrapper class to detect "UNKNOWN" package name and "0.0.0" package version, which although valid are almost surely mistakes.Hod

© 2022 - 2024 — McMap. All rights reserved.