Reputation: 702
I am looking for a way to specify my python program version (not the interpreter) exclusively in setup.cfg and use it at runtime in my python program - it appears to be the modern way to use only setup.cfg and omit setup.py altogether, as documented here: https://setuptools.pypa.io/en/latest/userguide/declarative_config.html
This topic seems quite involved with a lengthy discussion spanning years and no clear answer, as described here: Standard way to embed version into Python package?
What I'd like to achieve is that my setup.cfg has the version number (and all other relevant package data) and make that version available to my program at runtime.
Example setup.cfg
[metadata]
name = my_app
version = 0.1.3
author = foo
...
and then in my runtime access that version number:
2022-02-04T23:01:28.177 INFO root::============= Welcome to my_app version 0.1.3, PID: 52296 =============
How can I achieve that ?
Upvotes: 0
Views: 647
Reputation: 168986
Two options I can think of – I'd go with the first.
setup.cfg
's attr:
support to read the version from the source[metadata]
version = attr:my_app.__version__
# ...
__version__ = '0.1.3'
importlib.metadata
to read the version from the installation metadata(New in Python 3.8, has backports for older Pythons)
from importlib.metadata import version
my_version = version('my_app')
Upvotes: 2