2

我基本上需要知道它安装并添加到的特定应用程序的版本

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

我知道我可以使用 pip freeze 。我知道当前虚拟环境中的应用程序版本。

问题是我想支持两个版本的 the_application。

像 settings.INSTALLED_APP['the_application'].get_version() 这样的东西就是我要找的东西......

4

3 回答 3

7

模块/应用程序通常会通过模块级__version__属性公开其版本。例如:

import gunicorn
print gunicorn.__version__ # Prints version

import haystack
print haystack.__version__ 

一些警告是有序的:

  • 不能保证;查看
  • 应用程序公开其版本的“格式”会有所不同。例如,上面的第一个打印打印'0.15.0'在我的测试系统上;第二个打印(2, 0, 0, 'beta')在同一系统上。
于 2012-10-30T18:39:13.457 回答
4

这取决于应用程序如何管理它的版本控制。例如django-tagging ,有一个VERSION可以检查的元组和一个get_version()返回字符串函数。因此,无论您想检查版本(在运行时实时),只需执行以下操作:

import tagging
print tagging.get_version() # or print tagging.VERSION for the tuple
于 2012-10-30T18:35:20.087 回答
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
于 2013-04-17T08:35:21.327 回答