Single-sourcing the package version

There are many techniques to maintain a single source of truth for the version number of your project:

  1. Read the file in and parse the version with a regex. Example ( from pip

    here = os.path.abspath(os.path.dirname(__file__))
    def read(*parts):
        with, *parts), 'r') as fp:
    def find_version(*file_paths):
        version_file = read(*file_paths)
        version_match ="^__version__ = ['\"]([^'\"]*)['\"]",
                                  version_file, re.M)
        if version_match:
        raise RuntimeError("Unable to find version string.")
       version=find_version("package", "")


    This technique has the disadvantage of having to deal with complexities of regular expressions.

  2. Use an external build tool that either manages updating both locations, or offers an API that both locations can use.

    Few tools you could use, in no particular order, and not necessarily complete: bump2version, changes, zest.releaser.

  3. Set the value to a __version__ global variable in a dedicated module in your project (e.g., then have read and exec the value into a variable.

    version = {}
    with open("...sample/") as fp:
        exec(, version)
    # later on we use: version['__version__']

    Example using this technique: warehouse.

  4. Place the value in a simple VERSION text file and have both and the project code read it.

    with open(os.path.join(mypackage_root_dir, 'VERSION')) as version_file:
        version =

    An advantage with this technique is that it’s not specific to Python. Any tool can read the version.


    With this approach you must make sure that the VERSION file is included in all your source and binary distributions (e.g. add include VERSION to your

  5. Set the value in, and have the project code use the pkg_resources API.

    import pkg_resources
    assert pkg_resources.get_distribution('pip').version == '1.2.0'

    Be aware that the pkg_resources API only knows about what’s in the installation metadata, which is not necessarily the code that’s currently imported.

    Example using this technique: setuptools.

  6. Set the value to __version__ in sample/ and import sample in

    import sample


    Although this technique is common, beware that it will fail if sample/ imports packages from install_requires dependencies, which will very likely not be installed yet when is run.

  7. Keep the version number in the tags of a version control system (Git, Mercurial, etc) instead of in the code, and automatically extract it from there using setuptools_scm.