1

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

Miro
  • 1,699
  • 2
  • 16
  • 24
  • Setting a version in some variable and passing that variable to setup() is not an option, because I am using release tool which automatically sets this version.... – Miro Aug 30 '17 at 11:38

1 Answers1

0

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
phd
  • 69,888
  • 11
  • 97
  • 133