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 successfully. But how do I check whether Django uses the 2.6 or 2.7 version and what version of modules Django uses?
34 Answers
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)
2 Comments
AttributeError: module 'django' has no attribute 'VERSION'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 Comments
python -c "import django; print(django.VERSION)" returns (1, 8, 5, 'final', 0)python -c "import django; print(django.__version__)" instead. It also returns '2.2.4' (it's just a call to get_version()) and is the standard followed by most other libraries because it's defined in PEP 8. It works since Django 1.8If you have installed the application:
$ django-admin --version
3.2.6
5 Comments
>> django.VERSION in python shell displays the right versiondjango-admin.py --version. Depending on how you've done your python installation, you may even need python django-admin.py --version.python3 -c "import django; print(django.get_version())" that will give you the actual django version installed>>> import django
>>> print(django.get_version())
1.6.1
I am using the IDLE (Python GUI).
Comments
If you have pip, you can also do a
pip freezeand 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
Comments
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
Comments
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
1 Comment
django.VERSION, which already comes as a tuple? I'm pretty sure doing django.VERSION >= (1, 8) will always work as intended.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
python -m django --versiondjango-admin --versionordjango-admin.py version./manage.py --versionorpython manage.py --versionpip freeze | grep Djangopython -c "import django; print(django.get_version())"python manage.py runserver --version
Django Shell Commands
import django django.get_version()ORdjango.VERSIONfrom django.utils import version version.get_version()ORversion.get_complete_version()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.)
Comments
Simply type python -m django --version or type pip freeze to see all the versions of installed modules including Django.
1 Comment
django-admin --version
python manage.py --version
pip freeze | grep django
4 Comments
python manage.py --version Note the double -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
Comments
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....
1 Comment
pip freeze shows a ton of extra crap if you've pip'd in a bunch of packages.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.
Comments
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 a 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)
Comments
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)
3 Comments
pip freeze / python -c "import <module>; <module>.VERSION". You can simply reference it: /path/to/env/bin/python -c "<command>" or if you want to install/use pip, same thing: /path/to/env/bin/pip freeze. I use this all the time, specially when im logged in as a root and all of my application code runs as www-data i do: sudo su www-data -c "/path/to/env/bin/pip install <module>" and not even blink. (i know this is almost 2 years later, and you probably know about it now -- this is more for the next guy)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
Comments
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)
Comments
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
Comments
To check the Django version installed on your Windows PC,
In a Terminal session, run:
py -m django --version
Or in a Python REPL:
import django
django.get_version()
To check the Django version installed in your active virtual environment (venv), run:
Django-admin --version
Note that the Django version installed on your PC might be different from the version installed in your active virtual environment.
1 Comment
import django; django.get_version() worked on both 3.x and 5.x Django. Also py -m django --version. Do watch capitalization!
python3 -m django --versiondjango-admin --versioncat src/requirements.txt | grep "Django=="or in your application's settings.py file, on the fifth line you'll see something likeGenerated by 'django-admin startproject' using Django x.x.x(for the user-specific question, the accepted answers are the way, but I found that this could also be useful).