TIL: Single source version package builds with uv (redux)
Briefly

TIL: Single source version package builds with uv (redux)
"Here's how [he demonstrated](https://adamj.eu/tech/2025/07/30/python-check-package-version-importlib-metadata-version/) I should be doing it instead. ```toml # pyproject.toml [project] name = "air" version = "0.25.0" # This is the source of truth for the version number ```"
"The way to check programmatically the version number is to rely not on someone setting `__version__` in the code, but rather to use the following technique: ```python from importlib.metadata import version version = version("air") print(version) # This will print "0.25.0" ``` Thanks for the tip, Adam! This is a much cleaner and tool friendly way to ensure that the version number is consistent across your package without having to manually update it in multiple places."
Set the package version under [project] in pyproject.toml as the single source of truth (example: version = "0.25.0"). Retrieve the version at runtime using importlib.metadata.version("package_name") rather than defining and updating a __version__ variable inside the code. This method keeps the version consistent across packaging tools and runtime checks, removes the need to update multiple locations before publishing, and is more tool-friendly and cleaner for package maintenance. Include the pyproject.toml entry and a small runtime check using importlib.metadata to read the declared version.
Read at https://daniel.feldroy.com
Unable to calculate read time
[
|
]