2

I basically need to know the version of an specific application that it is installed and added to

INSTALLED_APPS = (
    ...
    'the_application',
    ...
)

I know that I can use pip freeze. I know the version of the application in my current virtual environment.

The problem is I want to support two versions of the_application.

Something like settings.INSTALLED_APP['the_application'].get_version() would be what I am looking for...

toto_tico
  • 17,977
  • 9
  • 97
  • 116

3 Answers3

8

A module / app will typically expose its version via a module level __version__ attribute. For example:

import gunicorn
print gunicorn.__version__ # Prints version

import haystack
print haystack.__version__ 

Some caveats are in order:

  • It is not guaranteed; check
  • The "format" in which the app will expose its version will differ. For example, the first print above printed '0.15.0' on my test system; the second one printed (2, 0, 0, 'beta') on the same system.
Ngure Nyaga
  • 2,989
  • 1
  • 20
  • 30
  • See also: http://stackoverflow.com/questions/710609/checking-python-module-version-at-runtime and http://stackoverflow.com/questions/710609/checking-python-module-version-at-runtime – Ngure Nyaga Oct 30 '12 at 18:41
  • what a serendipity! haystack was exactly my problem!! – toto_tico Oct 30 '12 at 18:47
4

It depends on how the application has managed it versioning. For example django-tagging has a tuple VERSION that you can check and a get_version() to return the string function. So where ever you want to check the version (live at run time), just do:

import tagging
print tagging.get_version() # or print tagging.VERSION for the tuple
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
0

thanks Ngure Nyaga! Your answer helped me a bit further, but it does not tell me where to put the vesrion

This answer however does not tell me where to put this __version__

So I looked in to an open application, which version does show up in django debugtoolbar. I looked in to the django restframework code, there I found out:

the version is put in the __init__.py file

(see https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/init.py)

and it is put here as:

__version__ = '2.2.7'
VERSION = __version__  # synonym

And after this, in his setup.py, he gets this version from this __init__.py : see: https://github.com/tomchristie/django-rest-framework/blob/master/setup.py

like this:

import re

def get_version(package):
    """
    Return package version as listed in `__version__` in `init.py`.
    """
    init_py = open(os.path.join(package, '__init__.py')).read()
    return re.match("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1)

version = get_version('rest_framework')

When using buildout and zestreleaser:

By the way, IAm using buildout and zest.releaser for building and versioning.

In this case, above is a bit different (but basically the same idea):

see http://zestreleaser.readthedocs.org/en/latest/versions.html#using-the-version-number-in-setup-py-and-as-version

The version in setup.py is automatically numbered by setup.py, so in __init__.py you do:

import pkg_resources

__version__ = pkg_resources.get_distribution("fill in yourpackage name").version
VERSION = __version__  # synonym
michel.iamit
  • 5,788
  • 9
  • 55
  • 74