'Version automation with github actions

I started recently with learning poetry and templates with cookiecutter for my python projects.

In the template I use the developer of the template used a following __init__.py for the main package:

from importlib_metadata import PackageNotFoundError, version

__author__ = "Authors Name"
__email__ = "[email protected]"

# Used to automatically set version number from github actions
# as well as not break when being tested locally
try:
    __version__ = version(__package__)  # type: ignore
except PackageNotFoundError:  # pragma: no cover
    __version__ = "0.0.0"

What I can't really follow is how the try part is working. I tested different things and my basic unittests, which literally just check against the __version__, __author__ and __email__ variables, also run through. It seems that the interpreter always jumps into the try and __version__is than always "0.0.0".

In what situation is the return of version(__package__) not equal to "0.0.0"?

My instincts are that the return in my local dev environment of version(__package__) is always "0.0.0" and as soon as I install via pip from PyPi or PyPi-test the version is taken from tag I used during the release or pre-release.

I would be satisfied if someone could confirm my thoughts or correct me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source