Python: Project version reads properly in venv, but as an installed package, returns “app.exe, version ” instead

I am writing a simple python application that’s compiled with a hatch build system.
hatch.pypa.io

I have properly defined pyproject.toml and I have properly declared version as per documentation here:
hatch metadata docu

When using argparser and it’s --version argument, the version such as 1.0.1 gets returned in venv properly, but once it’s installled as a global package, with:
pip install -e .

Then called using commandline entrypoint, then the version is instead:

app.exe, version <class 'packaging.version.Version'>

I have tried various solutions for this such as:

versionParsed = importlib.metadata.version('app_name')
from pkg_resources import get_distribution
versionParsed = get_distribution('app name')

But despite those 2 approaches, one new the other old, the situation is the same, outside of venv, the version returned isn’t numerical, but instead:

app.exe, version <class 'packaging.version.Version'>

The way I defined argparser to get version is standard, as far as I’m aware:

ap.add_argument('--version', action='version',
                version='%(prog)s, version {version}'.format(version=versionParsed))

Leave a Comment