The `single-source` library provides a unified way to access a Python project's version directly from code, aligning with PEP 621-style projects. It aims to establish a single source of truth for version information, preventing inconsistencies that arise from manual updates across multiple files. The library supports Python 3.8 and newer versions and is actively maintained.
pip install single-sourceVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to retrieve the project version using `get_version`. It shows how to pass the path to your project's root directory (where `pyproject.toml` resides) and how to handle the `VersionNotFoundError` if the version cannot be found when `raise_exc=True` is specified. For a runnable example, a dummy `pyproject.toml` is created and cleaned up.
To explicitly raise a `VersionNotFoundError` when the version is not found, call `get_version(..., raise_exc=True)`. Implement proper error handling for `VersionNotFoundError` to manage cases where version retrieval fails.
Carefully construct the `pathlib.Path` object to correctly represent your project's root directory. For a package installed via `pip`, `importlib.metadata.version('your-package-name')` might be a more robust runtime solution, as `single-source` is primarily for development-time versioning.Ensure your project's environment uses Python 3.8 or a later version. Upgrade your Python interpreter if necessary.
Verify that `pyproject.toml` exists in the `path_to_pyproject_dir` you provided to `get_version()`. Ensure the `[project].version` key is correctly defined in `pyproject.toml`, or if using a dynamic version, that your build system is configured to generate it.
Install the library using `pip install single-source`. If using a virtual environment, ensure it is activated before installation.
Provide the necessary `pathlib.Path` argument to `get_version()`. For example: `get_version(Path(__file__).parent.parent)` to look two directories up from the current script.