Creating a __version__ attribute for python packages without getting into trouble

package, python, version

Solution

One solution is to define the `__version__` in your `__init__.py` file and read that from within the setup.py. This means you only have to change the version in one location. I wrote a small function that will do this:

from setuptools import setup
import re

def get_property(prop, project):
    result = re.search(r'{}\s*=\s*[\'"]([^\'"]*)[\'"]'.format(prop), open(project + '/__init__.py').read())
    return result.group(1)

project_name = 'YOUR PROJECT'
setup(
    ...,
    version = get_property('__version__', project_name),
    ...,
)

you can also use this to fetch things like `__author__` or anything else defined in the `__init__.py` file

Problem

After reading the python documentation (http://www.python.org/dev/peps/pep-0396/) I was more confused than before about how to set the `__version__` attribute for packages appropriately. It is mentioned to put them into the `setup.py` file, which kind of confuses me: it would not be available as `my_package.__version__`, would it? I ended up to import the version attribute from a separate file. So my "version" file usually is ``` my_package/ __init__.py my_module1/ ... my_module2/ ... my_module3/ ... info/ __init__.py version.py __version__ = '0.1.0' ``` and in the uppermost `__init__.py` I import the `__version__` attribute from `info.version` : `import __version__` so that one can get version number via ``` my_package.__version__ ``` I am just wondering if this is a "okay" approach, and if something speaks against doing it like this? I am looking forward to your opinions and suggestions!

Original source

Related problems