Using setuptools
and pbr
There is not a standard way to manage version, but the standard way to manage your packages is setuptools
.
The best solution I've found overall for managing version is to use setuptools
with the pbr
extension. This is now my standard way of managing version.
Setting up your project for full packaging may be overkill for simple projects, but if you need to manage version, you are probably at the right level to just set everything up. Doing so also makes your package releasable at PyPi so everyone can download and use it with Pip.
PBR moves most metadata out of the setup.py
tools and into a setup.cfg
file that is then used as a source for most metadata, which can include version. This allows the metadata to be packaged into an executable using something like pyinstaller
if needed (if so, you will probably need this info), and separates the metadata from the other package management/setup scripts. You can directly update the version string in setup.cfg
manually, and it will be pulled into the *.egg-info
folder when building your package releases. Your scripts can then access the version from the metadata using various methods (these processes are outlined in sections below).
When using Git for VCS/SCM, this setup is even better, as it will pull in a lot of the metadata from Git so that your repo can be your primary source of truth for some of the metadata, including version, authors, changelogs, etc. For version specifically, it will create a version string for the current commit based on git tags in the repo.
As PBR will pull version, author, changelog and other info directly from your git repo, so some of the metadata in setup.cfg
can be left out and auto generated whenever a distribution is created for your package (using setup.py
)
Get the current version in real-time
setuptools
will pull the latest info in real-time using setup.py
:
python setup.py --version
This will pull the latest version either from the setup.cfg
file, or from the git repo, based on the latest commit that was made and tags that exist in the repo. This command doesn't update the version in a distribution though.
Updating the version metadata
When you create a distribution with setup.py
(i.e. py setup.py sdist
, for example), then all the current info will be extracted and stored in the distribution. This essentially runs the setup.py --version
command and then stores that version info into the package.egg-info
folder in a set of files that store distribution metadata.
Note on process to update version meta-data:
If you are not using pbr to pull version data from git, then just update your setup.cfg directly with new version info (easy enough, but make sure this is a standard part of your release process).
If you are using git, and you don't need to create a source or binary distribution (using python setup.py sdist
or one of the python setup.py bdist_xxx
commands) the simplest way to update the git repo info into your <mypackage>.egg-info
metadata folder is to just run the python setup.py install
command. This will run all the PBR functions related to pulling metadata from the git repo and update your local .egg-info
folder, install script executables for any entry-points you have defined, and other functions you can see from the output when you run this command.
Note that the .egg-info
folder is generally excluded from being stored in the git repo itself in standard Python .gitignore
files (such as from Gitignore.IO), as it can be generated from your source. If it is excluded, make sure you have a standard "release process" to get the metadata updated locally before release, and any package you upload to PyPi.org or otherwise distribute must include this data to have the correct version. If you want the Git repo to contain this info, you can exclude specific files from being ignored (i.e. add !*.egg-info/PKG_INFO
to .gitignore
)
Accessing the version from a script
You can access the metadata from the current build within Python scripts in the package itself. For version, for example, there are several ways to do this I have found so far:
## This one is a new built-in as of Python 3.8.0 should become the standard
from importlib.metadata import version
v0 = version("mypackage")
print('v0 {}'.format(v0))
## I don't like this one because the version method is hidden
import pkg_resources # part of setuptools
v1 = pkg_resources.require("mypackage")[0].version
print('v1 {}'.format(v1))
# Probably best for pre v3.8.0 - the output without .version is just a longer string with
# both the package name, a space, and the version string
import pkg_resources # part of setuptools
v2 = pkg_resources.get_distribution('mypackage').version
print('v2 {}'.format(v2))
## This one seems to be slower, and with pyinstaller makes the exe a lot bigger
from pbr.version import VersionInfo
v3 = VersionInfo('mypackage').release_string()
print('v3 {}'.format(v3))
You can put one of these directly in your __init__.py
for the package to extract the version info as follows, similar to some other answers:
__all__ = (
'__version__',
'my_package_name'
)
import pkg_resources # part of setuptools
__version__ = pkg_resources.get_distribution("mypackage").version
setup.py
is enough. See this question. – Gentillesetup.py
has a version (which it has to), why doesn'tpackage_name.__version__
just work, and show that version? – Centering