nbgv-python Package Overview
Overview
nbgv-pythonwraps the Nerdbank.GitVersioning CLI so Python projects can reuse the same versioning semantics.- The package discovers the CLI via
NBGV_PYTHON_COMMAND, a directnbgvexecutable, ordotnet tool run nbgvas a fallback. - A Hatch version source plugin is provided, allowing projects to declare
dynamic = ["version"]and resolve their version at build time.
Installation
-
Install from PyPI for programmatic use or CLI forwarding:
pip install nbgv-python
Or with
uv:uv add --dev nbgv-python # or install the CLI wrapper globally: uv tool install nbgv-python
-
For Hatch-managed projects, declare
nbgv-pythonin[build-system].requiresalongsidehatchling(see Hatch Integration). -
Ensure the
nbgvCLI is available either as a global .NET tool (dotnet tool install -g nbgv) or via a local.config/dotnet-tools.jsonmanifest. -
Optionally set
NBGV_PYTHON_COMMANDwhen the executable lives outside ofPATHor requires custom arguments.
Hatch Integration
Add the plugin to your project configuration:
[build-system]
requires = ["hatchling", "nbgv-python"]
build-backend = "hatchling.build"
[project]
dynamic = ["version"]
[tool.hatch.version]
source = "nbgv"
[tool.hatch.version.nbgv]
version-field = "SemVer2"
During version resolution Hatch invokes nbgv get-version --format json --project <absolute-project-path> and uses the selected field for the package version. The subprocess retains the caller's working directory.
Additional configuration keys include:
command(override CLI invocation)working-directory(project path passed to--project; relative paths resolve against the Hatch project root, and absolute paths are accepted)epoch(default: unset) – when provided, this non-negative integer is prepended to the normalized version to emit a PEP 440 epoch (for example2!1.2.3). Omit the key in TOML to skip the epoch; the Python configuration parser also acceptsNone. TOML has nonullvalue.
The plugin normalizes the chosen value to a PEP 440 compliant version, so SemVer pre-release tags such as -beta.1 are mapped to b1 automatically.
Template Fields
-
Extend the available template variables via the optional
template-fieldstable. Supported options and defaults are:version-tuple.mode(default:"nbgv") – choose how to build the tuple. Use"nbgv"(default) to assemble components from the Nerdbank.GitVersioning JSON payload, or"pep440"to parse the normalized version string.version-tuple.fields(default:["VersionMajor", "VersionMinor", "BuildNumber", "PrereleaseVersionNoLeadingHyphen"]) – whenmode = "nbgv", this ordered list determines which NBGV variables populate the tuple. Empty strings andnullvalues are skipped.version-tuple.normalized-prerelease(default:false) – whenmode = "nbgv", convert thePrereleaseVersionNoLeadingHyphenentry to its PEP 440 shorthand (for examplebeta.1→b1).version-tuple.epoch(default: unset) – only applies whenmode = "pep440". Omit it to emit the epoch only when the source string already includes a non-zero epoch (e.g.,2!1.0.0), settrueto force inclusion, orfalseto suppress it. The parser represents the unset value asNone.version-tuple.double-quote(default:true) – controls whether string parts in the tuple use double quotes ("") or single quotes ('').
[tool.hatch.version.nbgv.template-fields.version-tuple] fields = [ "VersionMajor", "VersionMinor", "BuildNumber", "PrereleaseVersionNoLeadingHyphen", ] double-quote = false
[tool.hatch.version.nbgv.template-fields.version-tuple] mode = "pep440" epoch = true
[tool.hatch.version.nbgv.template-fields.version-tuple] normalized-prerelease = true
[tool.hatch.version.nbgv] epoch = 2
-
The generated mapping includes
version,normalized_version,version_tuple, and every key exposed bynbgv get-version.versionrelays the raw value selected byversion-field(defaults toSemVer2). Use this if you need the exact string returned by NBGV (e.g.1.2.3-beta.1).normalized_versionapplies PEP 440 normalization to that value usingpackaging.version.Versionso1.2.3-beta.1becomes1.2.3b1. Use this for Python__version__strings.version_tupleexpands into a Python tuple using either the selected NBGV variables (mode = "nbgv", respectingnormalized-prereleasewhen enabled) or the parsed PEP 440 segments (mode = "pep440").
Writing Version Files
-
Emit a templated artifact (for example
src/pkg/_version.py) when Hatch resolves the version, including during builds. Thewritetable accepts the following keys:file(required) – relative or absolute path of the generated file.template(optional) – custom format string. When omitted,.pyfiles default to__version__ = "{normalized_version}"when that field is available (otherwiseversionis used), while.txtor extension-less files default to{version}; other suffixes require an explicit template.encoding(optional, default:"utf-8") – text encoding used when writing the file.
[tool.hatch.version.nbgv.write] file = "src/pkg/_version.py" template = "__version__ = '{normalized_version}'"
-
When
templateis omitted the plugin mirrorsversioningit:.pyfiles render__version__ = "{normalized_version}"(falling back toversionif normalization was not provided), while.txtor extension-less files render{version}. -
The target path is resolved relative to the project root and created on demand; remember to include it in your source distribution configuration if needed.
Python API
Retrieve version metadata directly from Python code:
from nbgv_python import get_version
version = get_version()
print(version["semver2"]) # -> 1.2.3+gabcdef
Forward arbitrary commands to the CLI:
from nbgv_python import forward
forward(["cloud", "--ci"])
Command discovery raises NbgvNotFoundError when it finds neither an override, nbgv, nor dotnet; subprocess failures with non-zero exit codes raise NbgvCommandError. An override is not checked for executability during discovery, and finding dotnet does not establish that the local nbgv tool is installed. get_version() raises NbgvJsonError for malformed JSON. The Hatch adapter wraps NbgvError failures from command discovery, version retrieval, and normalization in RuntimeError, retaining the original exception as its cause.
Command-Line Usage
The console script nbgv-python proxies all arguments to the real CLI:
nbgv-python get-version --format json
The wrapper surfaces the same exit codes as the underlying tool and prints diagnostic guidance with exit code 127 when discovery raises NbgvNotFoundError.
Project Records
- Architecture overview: CLI delegation, component boundaries, and unresolved differences from the original design.
- Hatch integration design: configuration rationale, error handling, and the existing validation basis.
- Changelog: package history and compatibility changes.
- Separate Hatch sample: sample-specific configuration and limitations.
Metadata
Release files for nbgv-python 2.1.0
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 |
|---|---|---|---|---|
| nbgv_python-2.1.0.tar.gz | 48.6 kB | Without provenance | Details for nbgv_python-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nbgv_python-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for nbgv_python-2.1.0-py3-none-any.whl |
Total release size: 83.7 kB
Release files / nbgv_python-2.1.0.tar.gz
| Download URL | nbgv_python-2.1.0.tar.gz |
|---|---|
| Size | 48.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e37fc98a58f147697b62cec6d9646c411973026a1b7e2df520412e8c81a2d416
|
|
BLAKE2b-256 checksum How to use checksums |
a69714376e1ec12683dd62fe2d5d8f88343e3c966054dd6ee1b8e9486326eb0d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / nbgv_python-2.1.0-py3-none-any.whl
| Download URL | nbgv_python-2.1.0-py3-none-any.whl |
|---|---|
| Size | 35.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c63eb1a317ddf25ae9d444a4ed0356d8077501911288380408418d6671f5e103
|
|
BLAKE2b-256 checksum How to use checksums |
213f55cac02f2447a8ac1d3e48083f69eb221d067819d68528a2633941bccfbb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for nbgv-python 2.1.0
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.