How can I get the version defined in setup.py (setuptools) in my package? How can I get the version defined in setup.py (setuptools) in my package? python python

How can I get the version defined in setup.py (setuptools) in my package?


Interrogate version string of already-installed distribution

To retrieve the version from inside your package at runtime (what your question appears to actually be asking), you can use:

import pkg_resources  # part of setuptoolsversion = pkg_resources.require("MyProject")[0].version

Store version string for use during install

If you want to go the other way 'round (which appears to be what other answer authors here appear to have thought you were asking), put the version string in a separate file and read that file's contents in setup.py.

You could make a version.py in your package with a __version__ line, then read it from setup.py using execfile('mypackage/version.py'), so that it sets __version__ in the setup.py namespace.

If you want a much simpler way that will work with all Python versions and even non-Python languages that may need access to the version string:

Store the version string as the sole contents of a plain text file, named e.g. VERSION, and read that file during setup.py.

version_file = open(os.path.join(mypackage_root_dir, 'VERSION'))version = version_file.read().strip()

The same VERSION file will then work exactly as well in any other program, even non-Python ones, and you only need to change the version string in one place for all programs.

Warning about race condition during install

By the way, DO NOT import your package from your setup.py as suggested in another answer here: it will seem to work for you (because you already have your package's dependencies installed), but it will wreak havoc upon new users of your package, as they will not be able to install your package without manually installing the dependencies first.


example study: mymodule

Imagine this configuration:

setup.pymymodule/        / __init__.py        / version.py        / myclasses.py

Then imagine some usual scenario where you have dependencies and setup.py looks like:

setup(...    install_requires=['dep1','dep2', ...]    ...)

And an example __init__.py:

from mymodule.myclasses import *from mymodule.version import __version__

And for example myclasses.py:

# these are not installed on your system.# importing mymodule.myclasses would give ImportErrorimport dep1import dep2

problem #1: importing mymodule during setup

If your setup.py imports mymodule then during setup you would most likely get an ImportError. This is a very common error when your package has dependencies. If your package does not have other dependencies than the builtins, you may be safe; however this isn't a good practice. The reason for that is that it is not future-proof; say tomorrow your code needs to consume some other dependency.

problem #2: where's my __version__ ?

If you hardcode __version__ in setup.py then it may not match the version that you would ship in your module. To be consistent, you would put it in one place and read it from the same place when you need it. Using import you may get the problem #1.

solution: à la setuptools

You would use a combination of open, exec and provide a dict for exec to add variables:

# setup.pyfrom setuptools import setup, find_packagesfrom distutils.util import convert_pathmain_ns = {}ver_path = convert_path('mymodule/version.py')with open(ver_path) as ver_file:    exec(ver_file.read(), main_ns)setup(...,    version=main_ns['__version__'],    ...)

And in mymodule/version.py expose the version:

__version__ = 'some.semantic.version'

This way, the version is shipped with the module, and you do not have issues during setup trying to import a module that has missing dependencies (yet to be installed).


The best technique is to define __version__ in your product code, then import it into setup.py from there. This gives you a value you can read in your running module, and have only one place to define it.

The values in setup.py are not installed, and setup.py doesn't stick around after installation.

What I did (for example) in coverage.py:

# coverage/__init__.py__version__ = "3.2"# setup.pyfrom coverage import __version__setup(    name = 'coverage',    version = __version__,    ...    )

UPDATE (2017): coverage.py no longer imports itself to get the version. Importing your own code can make it uninstallable, because you product code will try to import dependencies, which aren't installed yet, because setup.py is what installs them.