问题:如何检查Django版本

我必须为我们的应用程序使用PythonDjango。所以我有两个版本的Python,分别是2.6和2.7。现在,我已经安装了Django。我可以运行示例应用程序以测试Django成功。但是,如何确定Django使用2.6还是2.7版本以及Django使用什么版本的模块?

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 testing Django succesfuly. But how do I make sure whether Django uses the 2.6 or 2.7 version and what version of modules Django uses?


回答 0

Django 1.5支持Python 2.6.5和更高版本。

如果您使用的是Linux,并且要检查使用的Python版本,请从命令行运行python -V

如果要检查Django版本,请打开Python控制台并输入

>>> import django
>>> django.VERSION
(2, 0, 0, 'final', 0)

Django 1.5 supports Python 2.6.5 and later.

If you’re under Linux and want to check the Python version you’re using, run python -V from the command line.

If you want to check the Django version, open a Python console and type

>>> import django
>>> django.VERSION
(2, 0, 0, 'final', 0)

回答 1

基本上与bcoughlan的答案相同,但是在这里,它作为可执行命令:

$ python -c "import django; print(django.get_version())"
2.0

Basically the same as bcoughlan’s answer, but here it is as an executable command:

$ python -c "import django; print(django.get_version())"
2.0

回答 2

如果您已安装该应用程序:

$ django-admin.py version
2.0

If you have installed the application:

$ django-admin.py version
2.0

回答 3

转到Django项目主目录并执行以下操作:

./manage.py --version

Go to your Django project home directory and do:

./manage.py --version

回答 4

>>> import django
>>> print(django.get_version())
1.6.1

我正在使用IDLE(Python GUI)。

>>> import django
>>> print(django.get_version())
1.6.1

I am using the IDLE (Python GUI).


回答 5

如果您有点子,也可以

点冻结
它将显示您的所有组件版本,包括Django。

您可以通过grep传递它,以仅获取Django版本。那是,

josh@villaroyale:~/code/djangosite$ pip freeze | grep Django
Django==1.4.3

If you have pip, you can also do a

pip freeze
and it will show your all component version including Django .

You can pipe it through grep to get just the Django version. That is,

josh@villaroyale:~/code/djangosite$ pip freeze | grep Django
Django==1.4.3

回答 6

对于Python

import sys
sys.version

对于Django(如其他人在此处所述):

import django
django.get_version()

仅检查版本的潜在问题是版本会升级,因此代码可能会过时。您要确保’1.7′<‘1.7.1′<‘1.7.5′<‘1.7.10’。普通的字符串比较将在最后一次比较中失败:

>>> '1.7.5' < '1.7.10'
False

解决方案是使用distutils中的StrictVersion

>>> from distutils.version import StrictVersion
>>> StrictVersion('1.7.5') < StrictVersion('1.7.10')
True

For Python:

import sys
sys.version

For Django (as mentioned by others here):

import django
django.get_version()

The potential problem with simply checking the version, is that versions get upgraded and so the code can go out of date. You want to make sure that ‘1.7’ < ‘1.7.1’ < ‘1.7.5’ < ‘1.7.10’. A normal string comparison would fail in the last comparison:

>>> '1.7.5' < '1.7.10'
False

The solution is to use StrictVersion from distutils.

>>> from distutils.version import StrictVersion
>>> StrictVersion('1.7.5') < StrictVersion('1.7.10')
True

回答 7

正如您所说的,您有两个版本的python,我假设它们位于不同的虚拟环境(例如venv)或conda 环境中

当您安装django时,可能仅在一种环境中。可能有两个不同版本的django,每个版本的python都有一个。

在Unix / Mac终端中,您可以如下检查python版本:

$ python --version

如果您想知道来源:

$ which python

并检查django的版本:

$ python -m django --version

As you say you have two versions of python, I assume they are in different virtual environments (e.g. venv) or perhaps conda environments.

When you installed django, it was likely in only one environment. It is possible that you have two different versions of django, one for each version of python.

In from a Unix/Mac terminal, you can check your python version as follows:

$ python --version

If you want to know the source:

$ which python

And to check the version of django:

$ python -m django --version

回答 8

有多种获取Django版本的方法。您可以根据需要使用以下给出的任何一种。

注意: 如果您在虚拟环境中工作,请加载python环境


终端命令

  1. python -m django --version
  2. django-admin --version 要么 django-admin.py version
  3. ./manage.py --version 要么 python manage.py --version
  4. pip freeze | grep Django
  5. python -c "import django; print(django.get_version())"
  6. python manage.py runserver --version

Django Shell命令

  1. import django django.get_version() 要么 django.VERSION
  2. from django.utils import version version.get_version() 要么 version.get_complete_version()
  3. import pkg_resources pkg_resources.get_distribution('django').version

(如果您有某种更正或想添加更多相关信息,请随意修改此答案。)

There are various ways to get the Django version. You can use any one of the following given below according to your requirements.

Note: If you are working in a virtual environment then please load your python environment


Terminal Commands

  1. python -m django --version
  2. django-admin --version or django-admin.py version
  3. ./manage.py --version or python manage.py --version
  4. pip freeze | grep Django
  5. python -c "import django; print(django.get_version())"
  6. python manage.py runserver --version

Django Shell Commands

  1. import django django.get_version() OR django.VERSION
  2. from django.utils import version version.get_version() OR version.get_complete_version()
  3. import pkg_resources pkg_resources.get_distribution('django').version

(Feel free to modify this answer, if you have some kind of correction or you want to add more related information.)


回答 9

要使用Python Shell进行检查,请执行以下操作。

>>>from django import get_version
>>> get_version()

如果您希望在Unix / Linux shell中使用一行来完成此操作,请执行

python -c 'import django; print(django.get_version())'

开发应用程序后,即可使用以下方法直接检查版本。

python manage.py runserver --version

For checking using a Python shell, do the following.

>>>from django import get_version
>>> get_version()

If you wish to do it in Unix/Linux shell with a single line, then do

python -c 'import django; print(django.get_version())'

Once you have developed an application, then you can check version directly using the following.

python manage.py runserver --version

回答 10

django-admin --version
python manage.py --version
pip freeze | grep django
django-admin --version
python manage.py --version
pip freeze | grep django

回答 11

Django将使用PYTHONPATH环境变量指定的Python版本。您可以echo $PYTHONPATH在外壳中使用以确定要使用的版本。

Django使用的模块版本将是PYTHONPATH指定的Python版本下安装的模块版本。

Django will use the version of Python specified by the PYTHONPATH environment variable. You can use echo $PYTHONPATH in a shell to determine which version will be used.

The module versions used by Django will be the module versions installed under the version of Python specified by PYTHONPATH.


回答 12

pip list在LINUX TERMINAL上运行,并在列表中找到Django及其版本

pip freeze在Windows上的cmd上 运行

run pip list on LINUX TERMINAL and find Django and its version on list

run pip freeze on cmd on Windows


回答 13

您也可以不用Python就可以做到。只需在Django目录中输入以下内容即可:

cat __init__.py | grep VERSION

你会得到类似的东西:

VERSION = (1, 5, 5, 'final', 0)

You can do it without Python too. Just type this in your Django directory:

cat __init__.py | grep VERSION

And you will get something like:

VERSION = (1, 5, 5, 'final', 0)

回答 14

Django中有一个未记录的utils版本模块

https://github.com/django/django/blob/master/django/utils/version.py

有了它,您可以将正常版本作为字符串或详细版本元组获取:

>>> from django.utils import version
>>> version.get_version()
... 1.9
>>> version.get_complete_version()
... (1, 9, 0, 'final', 0)

There is an undocumented utils versions module in django

https://github.com/django/django/blob/master/django/utils/version.py

With that you can get the normal version as string or a detailed version tuple:

>>> from django.utils import version
>>> version.get_version()
... 1.9
>>> version.get_complete_version()
... (1, 9, 0, 'final', 0)

回答 15

Django版本或任何其他软件包版本

打开终端或命令提示符

类型

pip show django

要么

pip3 show django

您可以找到任何软件包版本…
示例

pip show tensorflow

pip show numpy 

等等….

Django version or any other package version

open the terminal or command prompt

type

pip show django

or

pip3 show django

you can find any package version…
example

pip show tensorflow

pip show numpy 

etc….


回答 16

输入您的CMD或终端:

python -m django --version

Type in your CMD or terminal:

python -m django --version

回答 17

我认为最Python化的方式是:

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

这直接与setup.py相关联: https //github.com/django/django/blob/master/setup.py#L37

这绝对是获得ANY软件包版本号的最佳方法!

也有 distutils

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

至于python版本,我同意@jamesdradbury

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

捆绑在一起:

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

I thought the most pythonic way was:

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

Its definitely the best way to get the version number of ANY package!

Also there is distutils

>>> 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 the python version, i agree with @jamesdradbury

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

回答 18

如果要进行Django版本比较,可以使用django-nine(pip install django-nine)。例如,如果您的环境中安装的Django版本是1.7.4,则以下内容将适用。

from nine import versions

versions.DJANGO_1_7 # True
versions.DJANGO_LTE_1_7 # True
versions.DJANGO_GTE_1_7 # True
versions.DJANGO_GTE_1_8 # False
versions.DJANGO_GTE_1_4 # True
versions.DJANGO_LTE_1_6 # False

If you want to make Django version comparison, you could use django-nine (pip install django-nine). For example, if Django version installed in your environment is 1.7.4, then the following would be true.

from nine import versions

versions.DJANGO_1_7 # True
versions.DJANGO_LTE_1_7 # True
versions.DJANGO_GTE_1_7 # True
versions.DJANGO_GTE_1_8 # False
versions.DJANGO_GTE_1_4 # True
versions.DJANGO_LTE_1_6 # False

回答 19

在Python Shell中输入以下命令

import django
django.get_version()

Type the following command in Python shell

import django
django.get_version()

回答 20

Django 1.0之后,您可以执行此操作

$ django-admin --version
1.11.10

After django 1.0 you can just do this

$ django-admin --version
1.11.10

回答 21

您可以通过在shell提示符中运行以下命令来获取Django版本

python -m django –version

如果安装了Django,则应该看到该版本,否则将收到错误消息,提示“没有名为django的模块”。

You can get django version by running the following command in a shell prompt

python -m django –version

If Django is installed, you should see the version otherwise you’ll get an error telling “No module named django”.


回答 22

您可以导入django,然后按如下所示键入print语句,以了解django的版本,即安装在系统上的版本:

>>> import django
>>> print(django.get_version())
2.1

you can import django and then type print statement as given below to know the version of django i.e. installed on your system:

>>> import django
>>> print(django.get_version())
2.1

回答 23

Django版本支持的Python版本

Django version     Python versions
1.0                2.3, 2.4, 2.5, 2.6
1.1                2.3, 2.4, 2.5, 2.6
1.2                2.4, 2.5, 2.6, 2.7
1.3                2.4, 2.5, 2.6, 2.7
1.4                2.5, 2.6, 2.7
1.5                2.6.5, 2.7 and 3.2.3, 3.3 (experimental)
1.6                2.6.5, 2.7 and 3.2.3, 3.3
1.11               2.7, 3.4, 3.5, 3.6, 3.7 (added in 1.11.17)
2.0                3.4, 3.5, 3.6, 3.7
2.1, 2.2           3.5, 3.6, 3.7

要验证Python是否可以看到Django,请在您的shell中键入python。然后在Python提示符下,尝试导入Django:

>>> import django
>>> print(django.get_version())
2.1
>>> django.VERSION
(2, 1, 4, 'final', 0)

Python version supported by Django version

Django version     Python versions
1.0                2.3, 2.4, 2.5, 2.6
1.1                2.3, 2.4, 2.5, 2.6
1.2                2.4, 2.5, 2.6, 2.7
1.3                2.4, 2.5, 2.6, 2.7
1.4                2.5, 2.6, 2.7
1.5                2.6.5, 2.7 and 3.2.3, 3.3 (experimental)
1.6                2.6.5, 2.7 and 3.2.3, 3.3
1.11               2.7, 3.4, 3.5, 3.6, 3.7 (added in 1.11.17)
2.0                3.4, 3.5, 3.6, 3.7
2.1, 2.2           3.5, 3.6, 3.7

To verify that Django can be seen by Python, type python from your shell. Then at the Python prompt, try to import Django:

>>> import django
>>> print(django.get_version())
2.1
>>> django.VERSION
(2, 1, 4, 'final', 0)

回答 24

只需键入python -m django --version 或键入pip freeze即可查看已安装模块的所有版本,包括Django。

Simply type python -m django --version or type pip freeze to see all the versions of installed modules including Django.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。