2
votes

My goal:

I want to keep coherent versions in my GIT repository, my distribution on pypi repository, and in my source using the __version__ variable.


Details:

I tried to use pbr, which generates the distro version from git tags, so these two versions will be coherent. However, I cannot find out how to keep the __version__ variable coherent with them in my source. (There are several ways to fetch the version from source, but how will it be connected to git/distro?)

Is it possible to generate a version file (to parse form source) or directly modify the __version__ variable?

2

2 Answers

1
votes

Since Python 3.8 there is a solution directly in the standard library: __version__ = importlib.metadata.version('Example'). See Using importlib.metadata.

1
votes

Finally, I have found the solution in this post.

import pkg_resources  # part of setuptools
version = pkg_resources.require("MyProject")[0].version

UPDATE

This post shows another, more robust solution:

from pbr.version import VersionInfo

package_name='MyProject'
info = VersionInfo(package_name)

version = info.version_string()