This release is a pre-release and may not be stable for production use.
setuptools-git-metadata
Include your package's version, Git commit, branch and build timestamps in source distributions and wheels. Import those values through a committed, customisable _version.py.
The build tool requires Python 3.10.12+, 3.11.4+, or 3.12+, and setuptools 80 or later. Git is required when collecting commit information. The generated runtime loader supports Python 3.9 or later.
Configure your project
Add the build requirement and enable metadata generation:
[build-system]
requires = ["setuptools>=80", "setuptools-git-metadata"]
build-backend = "setuptools.build_meta"
[project]
name = "example"
version = "1.2.3"
[tool.setuptools-git-metadata]
Metadata is included automatically in every package detected by setuptools. To select specific packages, add their import names:
[tool.setuptools-git-metadata]
packages = ["example", "example.utilities"]
The declared project.version determines the package version. Projects that customise setuptools' build_py or sdist commands must integrate the metadata commands with their overrides.
This development version is available from source. Build its wheel with uv build --wheel, then replace the setuptools-git-metadata requirement with setuptools-git-metadata @ file:///ABSOLUTE_PATH/setuptools_git_metadata-0.1.0.dev0-py3-none-any.whl.
Add the runtime loader
Install the build tool in your development environment, then create a loader in each package where you want to import the version information:
python3 -m setuptools_git_metadata template --output src/example/_version.py
Commit the generated file. You can customise its display strings and add application helpers. The command refuses to overwrite an existing file. To print the template for comparison, run:
python3 -m setuptools_git_metadata template
Build your distributions using your usual setuptools build command. Each selected package receives _git_metadata.json automatically. Keep this generated file out of your development checkout.
Import version information from an installed package:
from example._version import BUILD_INFO
print(BUILD_INFO.VERSION_INFO_SHORT)
print(BUILD_INFO.BUILD_GIT_HASH)
BUILD_INFO is a frozen GitMetadata instance. Its VERSION_INFO_* strings and BUILD_DATETIME are computed properties.
To load another package explicitly, use GitMetadata.from_package("package_name").
Without generated metadata, string fields use __metadata_missing__ and source/build timestamps are zero. Nullable fields, including unavailable branch and dirty state, remain None. This includes editable installs from a development checkout.
Available metadata
| Runtime fields | Meaning |
|---|---|
PACKAGE_VERSION, BUILD_VERSION |
Declared project version |
BUILD_GIT_HASH, BUILD_GIT_HASH_SHORT |
Full Git commit hash and Git's unique abbreviation (at least eight characters) |
BUILD_GIT_BRANCH |
Branch name, or None when unavailable |
BUILD_GIT_DIRTY |
Whether selected source inputs have changed |
BUILD_GIT_COMMIT_TIMESTAMP |
Git commit time |
SOURCE_TIMESTAMP |
Original source metadata time |
SDIST_BUILD_TIMESTAMP |
Original source distribution build time |
WHEEL_BUILD_TIMESTAMP |
Wheel build time |
BUILD_TIMESTAMP, BUILD_DATETIME |
Current artifact build time |
BUILD_INFO also provides VERSION_INFO_SHORT, VERSION_INFO, VERSION_INFO_LONG and VERSION_INFO_FULL display strings.
Wheels built from a source distribution retain its version, commit, branch and source timestamps. Their wheel build time reflects the new build. Set SOURCE_DATE_EPOCH to a nonnegative integer to control the artifact build time.
A source tree without Git information or previously generated metadata produces unavailable Git fields. Invalid metadata and a version that conflicts with previously generated metadata cause the build to fail.
Develop and test
uv sync --group dev
uvx tox -e lint
uvx tox
Tox covers CPython 3.10–3.15 and PyPy 3.10–3.12. GitHub Actions runs the suite on Ubuntu, macOS and Windows.
Metadata
Release files for setuptools-git-metadata 0.1.0.dev0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| setuptools_git_metadata-0.1.0.dev0.tar.gz | 22.6 kB | Without provenance | Details for setuptools_git_metadata-0.1.0.dev0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| setuptools_git_metadata-0.1.0.dev0-py3-none-any.whl | Python 3 | none | any | Details for setuptools_git_metadata-0.1.0.dev0-py3-none-any.whl |
Total release size: 35.7 kB
Release files / setuptools_git_metadata-0.1.0.dev0.tar.gz
| Download URL | setuptools_git_metadata-0.1.0.dev0.tar.gz |
|---|---|
| Size | 22.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba1197b06cee146f9396ef78d115b3777d6898b3965437c2492adea6aabc3e3b
|
|
BLAKE2b-256 checksum How to use checksums |
b863a10fbf02f7618b41e3fcc406a2787eba8fa7fc0dbfa1fe3f9a22d27d9c36
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Release files / setuptools_git_metadata-0.1.0.dev0-py3-none-any.whl
| Download URL | setuptools_git_metadata-0.1.0.dev0-py3-none-any.whl |
|---|---|
| Size | 13.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
805b28bda522be628b3cd343aa485c60c4d8fe00f88a7ef95cf06b4d79433dee
|
|
BLAKE2b-256 checksum How to use checksums |
fd803016dc5d202245b51423c6ebdacb415a00e0bb8f9a6b0601fd2c9faf2a0d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Security for setuptools-git-metadata 0.1.0.dev0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.