Skip to main content

DiscoverVersion

This package automatically discovers version information for a package. It tries the following options to get the version of the package (in order):

  • Check the DISCOVER_VERSION environment variable
  • Inspect a PKG-INFO file
  • Ask git, if the current directory is a git-repository
  • Ask importlib.metadata

It is intended as a lightweight replacement for setuptools_scm.

Usage with meson-python

For projects using the meson-python build backend, add the following to your pyproject.toml:

[build-system]
requires = ["meson>=1.0.0", "meson-python>=0.13.0", "ninja", "DiscoverVersion>=0.4.0"]
build-backend = "mesonpy"

[project]
dynamic = ["version"]
dependencies = ["DiscoverVersion"]

[tool.meson-python.metadata]
version.provider = "DiscoverVersion.meson_python"

Then add the following to your toplevel __init__.py for runtime version access:

from DiscoverVersion import get_version

__version__ = get_version('my_package_name', __file__)

Usage with flit

For projects using the flit build backend, add the following lines to your pyproject.toml:

[build-system]
requires = ["flit_core>=3.2", "DiscoverVersion"]
build-backend = "flit_core.buildapi"

[project]
dynamic = ['version']
dependencies = ['DiscoverVersion']

Then add the following to your toplevel __init__.py:

from DiscoverVersion import get_version

__version__ = get_version('my_package_name', __file__)

Note that it is important to hard code the name of your package in the call to get_version. The __file__ argument is required for git-based version discovery to locate the repository.

Environment Variable Override

You can override version discovery by setting the DISCOVER_VERSION environment variable. This is useful in CI/CD pipelines where git may not be available or when building in isolated environments:

DISCOVER_VERSION=1.2.3 python -m build

Command Line Interface

DiscoverVersion provides a CLI for discovering and outputting version information:

# Print the discovered version
python -m DiscoverVersion

# Or use the entry point
discover-version

# Write version to a Python file
python -m DiscoverVersion --write-to version.py

# Write version to a plain text file (useful for Meson builds)
python -m DiscoverVersion --write-to version.txt --plain

# Specify a fallback version if discovery fails
python -m DiscoverVersion --fallback 0.0.0

# Disable specific discovery methods
python -m DiscoverVersion --no-git --no-env

CI/CD Example (GitHub Actions)

Here's an example of using DiscoverVersion in a GitHub Actions workflow with cibuildwheel:

- name: Get version from DiscoverVersion
  id: get_version
  run: |
    pip install DiscoverVersion
    echo "version=$(python -m DiscoverVersion)" >> $GITHUB_OUTPUT

- name: Build wheels
  uses: pypa/cibuildwheel@v2
  env:
    CIBW_ENVIRONMENT: DISCOVER_VERSION=${{ steps.get_version.outputs.version }}

Tests

Before being able to run tests, you need to execute

pip install -e .[test]

to editably install the code. Then run tests with:

pytest

Metadata

Release files for DiscoverVersion 0.4.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 DiscoverVersion 0.4.0
File Size Uploaded
discoverversion-0.4.0.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for DiscoverVersion 0.4.0
File Interpreter ABI Platform
discoverversion-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / discoverversion-0.4.0.tar.gz

Download URL discoverversion-0.4.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d6b6bcc202fc9d6d64334e7ae98de8e4693f1f62cf9df510f008e8351ab7a658
BLAKE2b-256 checksum
How to use checksums
5be167e5d75fd0810bb64427dc282c24d34e19f92cbd616841a91a1a6cc1fe84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / discoverversion-0.4.0-py3-none-any.whl

Download URL discoverversion-0.4.0-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d45830a3340aa94bbe115cae28f7a02a4f65802423c33e8c385bc44168f0555
BLAKE2b-256 checksum
How to use checksums
3c35fb5287385b518d7908226403da28af30bae02463822288834bb24977e7e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.2

2 release files

0.3.1

1 release file

0.3.0

1 release file

0.2.6

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.2

1 release file

0.1.1

1 release file

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