Single-sourcing the Project Version

Page Status:Complete
Last Reviewed:2014-12-24

There are a few techniques to store the version in your project code without duplicating the value stored in

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

    def read(*names, **kwargs):
            os.path.join(os.path.dirname(__file__), *names),
            encoding=kwargs.get("encoding", "utf8")
        ) 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: bumpversion, 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.

    Using execfile:

    # now we have a `__version__` variable
    # later on we use: __version__

    Using exec:

    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.

  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.

  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.