Skip to main content

nbgv-python Package Overview

Overview

  • nbgv-python wraps the Nerdbank.GitVersioning CLI so Python projects can reuse the same versioning semantics.
  • The package discovers the CLI via NBGV_PYTHON_COMMAND, a direct nbgv executable, or dotnet tool run nbgv as 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-python in [build-system].requires alongside hatchling (see Hatch Integration).

  • Ensure the nbgv CLI is available either as a global .NET tool (dotnet tool install -g nbgv) or via a local .config/dotnet-tools.json manifest.

  • Optionally set NBGV_PYTHON_COMMAND when the executable lives outside of PATH or 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 example 2!1.2.3). Omit the key in TOML to skip the epoch; the Python configuration parser also accepts None. TOML has no null value.

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-fields table. 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"]) – when mode = "nbgv", this ordered list determines which NBGV variables populate the tuple. Empty strings and null values are skipped.
    • version-tuple.normalized-prerelease (default: false) – when mode = "nbgv", convert the PrereleaseVersionNoLeadingHyphen entry to its PEP 440 shorthand (for example beta.1 → b1).
    • version-tuple.epoch (default: unset) – only applies when mode = "pep440". Omit it to emit the epoch only when the source string already includes a non-zero epoch (e.g., 2!1.0.0), set true to force inclusion, or false to suppress it. The parser represents the unset value as None.
    • 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 by nbgv get-version.

    • version relays the raw value selected by version-field (defaults to SemVer2). Use this if you need the exact string returned by NBGV (e.g. 1.2.3-beta.1).
    • normalized_version applies PEP 440 normalization to that value using packaging.version.Version so 1.2.3-beta.1 becomes 1.2.3b1. Use this for Python __version__ strings.
    • version_tuple expands into a Python tuple using either the selected NBGV variables (mode = "nbgv", respecting normalized-prerelease when 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. The write table accepts the following keys:

    • file (required) – relative or absolute path of the generated file.
    • template (optional) – custom format string. When omitted, .py files default to __version__ = "{normalized_version}" when that field is available (otherwise version is used), while .txt or 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 template is omitted the plugin mirrors versioningit: .py files render __version__ = "{normalized_version}" (falling back to version if normalization was not provided), while .txt or 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

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)

Source distribution for nbgv-python 2.1.0
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)

Table of built distributions (wheels) for nbgv-python 2.1.0
File Interpreter ABI Platform Actions
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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page