Can I use setuptools_scm to check at runtime whether the installed version is behind the Git version?

327 Views Asked by At

I have a Python package that is set up to retrieve the version number from Git tag when it is installed locally. All of this works really well.

This is what I have in my pyproject.toml:

[tool.setuptools_scm]
write_to = "my_package/version.py"
git_describe_command = "git describe --dirty --tags --long --match v* --first-parent"

Then I use this in the module that defines the command line client so that a user can run a command my_package --version:

try:
    from .version import version as __version__
    from .version import version_tuple
except ImportError:
    __version__ = "unknown version"
    version_tuple = (0, 0, "unknown version")

All of this works fine: it extracts the version of the package from the latest tag on the GitLab repo and writes it to the file "version.py" from which the above import can obtain the version and show it to the user.

My question is: is there a way I could check at runtime (not during setup) whether a newer version exists on the Git repo so I can tell the user to update? I would insert this check before any command run by the CLI so I can output a warning to upgrade before running the command or perhaps even fail with an exception to prevent users running old versions of a package that is under heavy development (all of this is local, not in a public repo).

I might have missed it but couldn't find anything about this in the docs for setuptools_scm but I reckon this is a very common use case so probably it's possible somehow? Thanks!

1

There are 1 best solutions below

0
On

Hope this is what you were looking for:

Citing carpentries-incubator

This can be retrieved at runtime by adding the following to init.py:

# file: __init__.py
from importlib.metadata import version, PackageNotFoundError
try:
    __version__ = version("epi_models") except PackageNotFoundError:
    # If the package is not installed, don't add __version__
    pass