How to access distutils version number?

I have a python package that I create with distutils and I use the version argument in my setup, for example (with other arguments besides the name and version):

setup(name='example_module', version='0.1.2', ...) 

I can extract the version number from the module in the example_module package by calling:

 import pkg_resources version = pkg_resources.require('example_module')[0].version 

How do I access this version number from a python script that is outside of this distribution?

+4
source share
2 answers

The easiest way to have a package version available in another python program outside the distribution kit is to follow PEP 396. http://www.python.org/dev/peps/pep-0396/

Any other script can access the python package version by running

 python setup.py --version 

and capture output. I use this in my Makefile to extract the version number when creating the online documentation of my packages.

If your package contains any command line utilities, make sure you enable the --version command line option. Then any script / program will be able to get the version of your package after installation using this.

+1
source

Typically, the version number is a constant in the package source, setup.py uses this value, and then any importing package can read the version string.

+1
source

All Articles