How to check Django version

后端 未结 26 2596
梦如初夏
梦如初夏 2020-12-04 04:36

I have to use Python and Django for our application. So I have two versions of Python, 2.6 and 2.7. Now I have installed Django. I could run the sample application for testi

26条回答
  •  一向
    一向 (楼主)
    2020-12-04 05:19

    The most pythonic way I've seen to get the version of any package:

    >>> import pkg_resources;
    >>> pkg_resources.get_distribution('django').version
    '1.8.4'
    

    This ties directly into setup.py: https://github.com/django/django/blob/master/setup.py#L37

    Also there is distutils to compare the version:

    >>> from distutils.version import LooseVersion, StrictVersion
    >>> LooseVersion("2.3.1") < LooseVersion("10.1.2")
    True
    >>> StrictVersion("2.3.1") < StrictVersion("10.1.2")
    True
    >>> StrictVersion("2.3.1") > StrictVersion("10.1.2")
    False
    

    As for getting the python version, I agree with James Bradbury:

    >>> import sys
    >>> sys.version
    '3.4.3 (default, Jul 13 2015, 12:18:23) \n[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]'
    

    Tying it all together:

    >>> StrictVersion((sys.version.split(' ')[0])) > StrictVersion('2.6')
    True
    

提交回复
热议问题