Retrieving python module own version from setup.py

510 Views Asked by At

I am writing Flask based module with standard setup.py in root directory:

#!/usr/bin/env python
from distutils.core import setup

setup(name=modulename,
      version='0.2.16.dev0',
      description='...',
      author='...',
      ...
     )

I am willing to expose module version using Flask API. What is the correct way to access my own module version programmatically?

Thanks

Update: I forgot to mention that the module is not necessary installed as a standard module and may not be available in PYTHONPATH. This is why this question is not like this and this

1

There are 1 best solutions below

0
On

Put version in version.py or __version__.py or such (inside the package) and import it both in setup.py and the application:

from distutils.core import setup
from mypackage.version import version

setup(…
      version=version,
      …
     )

If importing your package in setup.py causes unwanted side effects you can just read mypackage/version.py and parse it or exec() or import it alone (without the package) with the trick:

from imp import load_source
from os.path import abspath, dirname, join

versionpath = join(abspath(dirname(__file__)), "mypackage", "__version__.py")
load_source("mapackage_version", versionpath)
from mapackage_version import version