How could I get the version defined in setup.py
from my package (for --version
, or other purposes)?
With a structure like this:
setup.py
mymodule/
/ __init__.py
/ version.py
/ myclasses.py
where version.py contains:
__version__ = 'version_string'
You can do this in setup.py:
import sys
sys.path[0:0] = ['mymodule']
from version import __version__
This won't cause any problem with whatever dependencies you have in your mymodule/__init__.py
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
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.
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.
I wasn't happy with these answers... didn't want to require setuptools, nor make a whole separate module for a single variable, so I came up with these.
For when you are sure the main module is in pep8 style and will stay that way:
version = '0.30.unknown'
with file('mypkg/mymod.py') as f:
for line in f:
if line.startswith('__version__'):
_, _, version = line.replace("'", '').split()
break
If you'd like to be extra careful and use a real parser:
import ast
version = '0.30.unknown2'
with file('mypkg/mymod.py') as f:
for line in f:
if line.startswith('__version__'):
version = ast.parse(line).body[0].value.s
break
setup.py is somewhat of a throwaway module so not an issue if it is a bit ugly.
Update: funny enough I've moved away from this in recent years and started using a separate file in the package called meta.py
. I put lots of meta data in there that I might want to change frequently. So, not just for one value.
We wanted to put the meta information about our package pypackagery
in __init__.py
, but could not since it has third-party dependencies as PJ Eby already pointed out (see his answer and the warning regarding the race condition).
We solved it by creating a separate module pypackagery_meta.py
that contains only the meta information:
"""Define meta information about pypackagery package."""
__title__ = 'pypackagery'
__description__ = ('Package a subset of a monorepo and '
'determine the dependent packages.')
__url__ = 'https://github.com/Parquery/pypackagery'
__version__ = '1.0.0'
__author__ = 'Marko Ristin'
__author_email__ = 'marko.ristin@gmail.com'
__license__ = 'MIT'
__copyright__ = 'Copyright 2018 Parquery AG'
then imported the meta information in packagery/__init__.py
:
# ...
from pypackagery_meta import __title__, __description__, __url__, \
__version__, __author__, __author_email__, \
__license__, __copyright__
# ...
and finally used it in setup.py
:
import pypackagery_meta
setup(
name=pypackagery_meta.__title__,
version=pypackagery_meta.__version__,
description=pypackagery_meta.__description__,
long_description=long_description,
url=pypackagery_meta.__url__,
author=pypackagery_meta.__author__,
author_email=pypackagery_meta.__author_email__,
# ...
py_modules=['packagery', 'pypackagery_meta'],
)
You must include pypackagery_meta
into your package with py_modules
setup argument. Otherwise, you can not import it upon installation since the packaged distribution would lack it.