Hatch VCS Plugin
Hatch-vcs is a Hatch plugin that enables project versioning using your preferred Version Control System (VCS), such as Git or Mercurial. It integrates with Hatch's build system to dynamically determine the package version based on VCS tags. The current version is 0.5.0, and it maintains an active release cadence, frequently updating to support new Python versions and Hatchling features.
Warnings
- breaking Support for Python 3.8 was dropped in version 0.5.0. Projects using older Python versions will need to upgrade Python or pin an older `hatch-vcs` version.
- gotcha When developing with editable installs, the `_version.py` file generated by `hatch-vcs` is static and won't update automatically during development. The version derived from `importlib.metadata` can also become outdated. For runtime updates, `hatch-vcs` needs to be installed in the runtime environment and an environment variable (`MYPROJECT_HATCH_VCS_RUNTIME_VERSION`) can be set.
- gotcha Older versions of `hatch-vcs` (prior to v0.5.0) might emit deprecation warnings when using the `tag-pattern` option due to underlying dependency changes.
- gotcha Before version 0.4.0, a `UserWarning` could be emitted if a template was not explicitly defined when using certain `hatch-vcs` features.
Install
-
pip install hatch hatch-vcs -
# In pyproject.toml: [build-system] requires = ["hatchling>=1.27", "hatch-vcs>=0.3.0"] build-backend = "hatchling.build"
Imports
- hatch-vcs
No direct Python import. Configured via pyproject.toml.
Quickstart
# pyproject.toml
[project]
name = "my-package"
version = "0.0.1" # This is a fallback/initial value, will be overridden by VCS
dynamic = ["version"]
[build-system]
requires = ["hatchling>=1.27", "hatch-vcs>=0.3.0"]
build-backend = "hatchling.build"
[tool.hatch.version]
source = "vcs"
[tool.hatch.build.hooks.vcs]
version-file = "src/my_package/_version.py"
# Example of how you might read the version in your package (e.g., in src/my_package/__init__.py)
# from importlib.metadata import version
# try:
# __version__ = version("my-package")
# except Exception:
# # Fallback for development installs or if metadata is not yet available
# __version__ = "0.0.0+unknown"