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 the package into your monorepo environment using
uv add --package nbgv-python nbgv-python. - Ensure the
nbgvCLI is available either as a global .NET tool (dotnet tool install -g nbgv) or via a local tool manifest. - Optionally set
NBGV_PYTHON_COMMANDwhen the executable lives outside ofPATHor requires additional arguments.
Hatch Integration
Add the plugin to your project configuration:
[project]
dynamic = ["version"]
[tool.hatch.version]
source = "nbgv"
[tool.hatch.version.nbgv]
version-field = "SemVer2"
During builds Hatch invokes nbgv get-version --format json in the project root and uses the selected field for the package version.
Additional configuration keys include:
command(override CLI invocation)working-directory(relative path for the repository root)epoch(default:null) – when provided, this non-negative integer is prepended to the normalized version to emit a PEP 440 epoch (for example2!1.2.3). Leave it unset ornullto omit the epoch entirely.
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:null) – only applies whenmode = "pep440". Keep itnullto 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.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) during build. 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"])
All helpers raise NbgvNotFoundError when the CLI cannot be resolved and NbgvCommandError for non-zero exit codes.
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 when the command cannot be located.
Metadata
Release files for nbgv-python 2.0.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 | |
|---|---|---|---|---|
| nbgv_python-2.0.0.tar.gz | 29.4 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nbgv_python-2.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 48.1 kB
Release files / nbgv_python-2.0.0.tar.gz
| Download URL | nbgv_python-2.0.0.tar.gz |
|---|---|
| Size | 29.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1f5ba964119cbf0e5e63e6f1a71859a718669a8719f042b782f273dcdc04a74c
|
|
BLAKE2b-256 checksum How to use checksums |
6e8006f595b825833bb8f81ae69ac0796a5ad3ee80e69da980dc43543fe511c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 19, 2025.
Transparency logRelease files / nbgv_python-2.0.0-py3-none-any.whl
| Download URL | nbgv_python-2.0.0-py3-none-any.whl |
|---|---|
| Size | 18.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0d8fcd968c10aa301096c04954d797d73fd42539a18ba72fc98351ebe626944d
|
|
BLAKE2b-256 checksum How to use checksums |
52808f2f87f1d9a98ad713be38100407c9037f3e358bed9e8cdfdf9673249cb2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 19, 2025.
Transparency logSecurity for nbgv-python 2.0.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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for nbgv_python-2.0.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 19, 2025.
Transparency logA 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.