Versioning It
Versioningit is a Python packaging plugin that automatically determines your package's version based on tags in your version control repository (Git and Mercurial supported). It offers extensive customization for version formatting and allows overriding internal functions for version extraction and calculation. It integrates with both setuptools and Hatch. The current version is 3.3.0, and it has a regular release cadence with several updates throughout the year.
Warnings
- breaking Major versions of versioningit (e.g., v3.0.0) may introduce breaking changes, primarily to the library and custom method API. For example, v3.0.0 changed the `build_dir` argument for `Versioningit.do_onbuild()` and renamed the `version` argument to `base_version` in `Versioningit.do_format()`.
- gotcha Versioningit requires the entire repository history (or at least back to the most recent tag) to determine the version. Building or installing from shallow clones (common in CI environments) will not work correctly.
- gotcha Any explicit 'version' field in `setup.py`, `setup.cfg`, or `pyproject.toml` will be ignored and overwritten by `versioningit`.
- gotcha If using `pyproject.toml` for project metadata (PEP 621), you *must* explicitly declare `version` as dynamic by adding `dynamic = ["version"]` to your `[project]` table.
- gotcha During editable installs (`pip install -e .`), the version calculated by `versioningit` is only updated when the install command is re-run. Changes to VCS tags or repository state after the initial editable install will not be reflected until re-installation.
- gotcha If you define and use custom methods within your Python package, retrieving the package version using `importlib.metadata.version()` inside `__init__.py` might fail during the build process, as `versioningit` loads the package before it's fully installed.
- gotcha If `[tool.versioningit].default-version` is not set in `pyproject.toml` and an error occurs during version calculation (e.g., no tags found in repository), the build or install process will fail.
Install
-
pip install versioningit -
# In your pyproject.toml (for setuptools) [build-system] requires = ["setuptools", "versioningit"] build-backend = "setuptools.build_meta" # Or for Hatch [build-system] requires = ["hatchling", "versioningit"] build-backend = "hatchling.build" [tool.hatch.version] source = "versioningit"
Imports
- version
from importlib.metadata import version __version__ = version("your_package_name") - Versioningit
from versioningit import Versioningit
Quickstart
# pyproject.toml
[project]
name = "my_package"
version = "dynamic"
[tool.versioningit]
# Minimal configuration (uses defaults: Git, basic tag2version, basic format)
# To retrieve the version at runtime (e.g., in my_package/__init__.py):
# from importlib.metadata import version
# __version__ = version("my_package")