How can I get the version defined in setup.py (setuptools) in my package?
python, setuptools
Solution
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 setuptools
version = 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.
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.
Problem
How could I get the version defined in `setup.py` from my package (for `--version`, or other purposes)?
Related problems
- What is an alternative to execfile in Python 3?
- Standard way to embed version into Python package?
- How to include package data with setuptools/distutils?
- Is it possible to include subdirectories using dist utils (setup.py) as part of package data?
- python open file error
- How to use setuptools packages and ext_modules with the same name?
- Define a global in a Python module from a C API