【问题标题】:How to check the version of a installed application in Django in running time?如何在运行时检查 Django 中已安装应用程序的版本?
【发布时间】:2012-10-20 04:12:51
【问题描述】:

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

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

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

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

settings.INSTALLED_APP['the_application'].get_version() 之类的东西就是我要找的东西...

【问题讨论】:

    标签: django


    【解决方案1】:

    这取决于应用程序如何管理它的版本控制。例如django-tagginghas a tuple VERSION that you can check and a get_version() to return the string 函数。因此,无论您想检查版本(在运行时实时),只需执行以下操作:

    import tagging
    print tagging.get_version() # or print tagging.VERSION for the tuple
    

    【讨论】:

      【解决方案2】:

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

      import gunicorn
      print gunicorn.__version__ # Prints version
      
      import haystack
      print haystack.__version__ 
      

      请注意以下几点:

      • 不保证;检查
      • 应用程序公开其版本的“格式”会有所不同。例如,上面的第一个打印在我的测试系统上打印了'0.15.0';第二个在同一系统上打印(2, 0, 0, 'beta')

      【讨论】:

      【解决方案3】:

      感谢 Ngure Nyaga! 您的回答进一步帮助了我,但它并没有告诉我将 vesrion

      放在哪里

      但是这个答案并没有告诉我把这个__version__放在哪里

      所以我查看了一个打开的应用程序,它确实显示在 django 调试工具栏中。 我查看了 django restframework 代码,发现:

      版本放在__init__.py文件中

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

      它被放在这里:

      __version__ = '2.2.7'
      VERSION = __version__  # synonym
      

      在此之后,在他的 setup.py 中,他从 __init__.py 获得了这个版本: 见:https://github.com/tomchristie/django-rest-framework/blob/master/setup.py

      像这样:

      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')
      

      使用 buildout 和 zestreleaser 时:

      顺便说一句,IAm 使用 buildout 和 zest.releaser 进行构建和版本控制。

      在这种情况下,上面有点不同(但基本相同的想法):

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

      setup.py 中的版本由 setup.py 自动编号,所以在__init__.py 你这样做:

      import pkg_resources
      
      __version__ = pkg_resources.get_distribution("fill in yourpackage name").version
      VERSION = __version__  # synonym
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-19
        • 2020-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-18
        相关资源
        最近更新 更多