Python Single-Source Versioning Library

0.4.0 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

from pathlib import Path
from single_source import get_version, VersionNotFoundError

# For demonstration, assume a pyproject.toml exists in the parent directory.
# In a real project, this path would point to your project root.
try:
    # Adjust path_to_pyproject_dir to point to your project's root.
    # For an executable script, .parent.parent might work if pyproject.toml is two levels up.
    # For a library in a package, it might be Path(__file__).parent.parent.parent
    project_root = Path(__file__).resolve().parent
    # In a typical project, if your script is in `src/my_package/my_module.py` and pyproject.toml is at project root:
    # project_root = Path(__file__).resolve().parents[3] # Adjust as needed
    
    # Example of a simplified path for a quick demonstration setup:
    # Create a dummy pyproject.toml for this example to be runnable
    dummy_project_dir = Path('./dummy_project_for_single_source_test')
    dummy_project_dir.mkdir(exist_ok=True)
    pyproject_content = '[project]\nname = "my_test_package"\nversion = "1.2.3"\n'
    (dummy_project_dir / 'pyproject.toml').write_text(pyproject_content)

    # Now get the version from the dummy project
    version = get_version(dummy_project_dir)
    print(f"Project version (from dummy_project): {version}")

    # Example with explicit error raising
    # Clean up dummy project
    (dummy_project_dir / 'pyproject.toml').unlink()
    dummy_project_dir.rmdir()

    # Attempt to get version from a non-existent path to demonstrate VersionNotFoundError
    non_existent_path = Path('./non_existent_project')
    print(f"\nAttempting to get version from non-existent path '{non_existent_path}' with error raising...")
    try:
        get_version(non_existent_path, raise_exc=True)
    except VersionNotFoundError as e:
        print(f"Caught expected error: {e}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Ensure cleanup even if errors occur before the explicit unlink/rmdir
    dummy_project_dir = Path('./dummy_project_for_single_source_test')
    if dummy_project_dir.exists():
        if (dummy_project_dir / 'pyproject.toml').exists():
            (dummy_project_dir / 'pyproject.toml').unlink()
        if dummy_project_dir.is_dir():
            dummy_project_dir.rmdir()

view raw JSON →