Version Numbering in Python Package

After written the short Python package post I realized that the version number (actually string but anyway) of the package is now in two different places: in the package code ( and in the package install metadata ( Obviously that’s not ideal, so I tried to figure out a way to define the version in only one place, and here’s my attempt.

First, in the same directory as the package code, create with the following contents:

__version__ = "1.2.dev1"

(For the version numbering scheme, see PEP 440 — Version Identification and Dependency Specification.)

Then modify the package code in

from ._version import __version__ as _version
__version__ = _version

def get_stuff():
    return "This is stuff. Use it wisely."

Finally, recreate

from setuptools import setup, find_packages
from stuff._version import __version__

        description="The Stuff package by Me",

This way the version number is specified in only one file ( Here is how the module usage looks like:

$ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import stuff
>>> stuff.__version__
>>> stuff.get_stuff()
'This is stuff. Use it wisely.'

Now, the package name (stuff) is still mentioned twice in once in version information import and once in the name argument for the setup function. They can be handled in also like this:

from setuptools import setup, find_packages
from importlib import import_module

PKG_NAME = "stuff"

module = import_module("{}._version".format(PKG_NAME))
        description="The Stuff package by Me",


If you need to compare version strings, you can do it like this:

from distutils.version import LooseVersion as version
import stuff

if version(stuff.__version__) < version("1.3"):
    print("Sorry, I need at least version 1.3 of stuff")

Updated: June 29, 2019 — 20:15

Leave a Reply