Working with versions generated from git repo tags by pbr
I'm having issues getting the version info from my package, which is setup as a normal Python package using setuptools
with the pbr
extension. pbr
will pull the version info from the tags in a git repo so there is a single source of truth.
I did get the basics working - see this Q&A for more info.
It is working overall, if I make a full distribution of the package. For example: py setup.py sdist
will create a full distribution and the version tag and other meta data from git is updated.
But I don't need full distributions of this package, my team is just working on it locally out of a git repo. I'm also using pyinstaller
to create an executable of the main script for user distribution (without needing Python). So I want a simple, quick command that will just update the metadata, based on the latest updated to the git repo, without spending extra time for SetupTools to build and create the full distribution package, which will not be used anyway.
pbr
will pull version (from tags), authors, and changelog info from the repo, so it's a big timesaver, allowing us to keep most of the metadata directly in git.
Questions
Is there anything we can do to get the package metadata to update with pbr
, without making a full distribution?
Particularly on the version:
- When or with which commands will
setup.py
using thepbr
extension actually go update the version from the git repo so that any command querying the version will get the updated version? - Where is the version actually stored in this setup? (I can't find it...)
More info and some code
I've got a very short setup.py
, for use with pbr
extension:
#!/usr/bin/env python
from setuptools import setup
setup(
setup_requires=['pbr', 'setuptools'],
pbr=True,
)
Now, if I update the latest commit in the git repo with a tag, the command py setup.py --version
will return the new updated version based on that tag, but any other direct method I can find will still return the old version string. So that command does not appear to store the new version string anywhere.
Here's a few methods I've tried from my package __init__.py
:
import pkg_resources # part of setuptools
v1 = pkg_resources.require("md2mat")[0].version
print('v1 {}'.format(v1))
v2 = pkg_resources.get_distribution('md2mat').version
print('v2 {}'.format(v2))
from pbr.version import VersionInfo
v3 = VersionInfo('md2mat').release_string()
print('v3 {}'.format(v3))
# Update per sinoroc's comment:
# As of Python 3.8, you can use this from the stdlib,
# which removes run-time dependencies on `pbr` or `setuptools`
import importlib.metadata
__version__ = importlib.metadata.version('Example')
All of the above return the old version string (last time a full distribution was made).
So, is there anything I can do short of py setup.py sdist
or a similar full distribution build command (bdist, bdist_egg, etc.) to simply update my package info in place so the above methods will give me the latest version string and other git metadata when the main package script is run?
That would allow us to update the package locally as developers after checking out a new commit, and to then run pyinstaller
to create our exe and release our end user version of the script.
import importlib.metadata; __version__ = importlib.metadata.version('Example')
since it's in the standard library since Python 3.8. It prevents from having run time dependencies (install_requires
) onsetuptools
orpbr
. – Silvanpkg_resources
methods to work withpyinstaller
... thanks for the tip. – Debusimportlib-metadata
working with Python 3.8.0, but PyInstaller wouldn't successfully compile my package, so I'm back on Python 3.7. I believe all the other information and steps are still required, so while this provides a cleaner method for accessing the metadata in scripts, it's only a nice-to-have for the moment, that I can't use at the moment. – Debus