TIL: Single source version package builds with uv
Tired of updating the version in multiple places before publishing a package update? Leery of using inspect.metadata to fetch the package? Here's how to have a single source of version using UV's build subcommand.
- Remove
versioninpyproject.tomland replace withdynamic = ["version"] - Add
[tool.setuptools.dynamic]and specify the location of the version using this dialogue:version = { attr = "mypackage.__version__" } - In your project's root
__init__.py, add a__version__attribute.
Example:
# pyproject.toml
[project]
name = "mypackage"
dynamic = ["version"]
# version = "0.1.0" # Don't set version here
[tool.setuptools.dynamic]
version = { attr = "mypackage.__version__" } # Set version here
Don't forget to specify the version in Python:
# mypackage/__init__.py
__version__ = "0.1.0"
Thanks to Audrey Roy Greenfeld for pairing with me on getting this to work.

