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?
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)
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
If you have installed the application:
$ django-admin --version
3.2.6
Go to your Django project home directory and do:
./manage.py --version
>>> import django
>>> print(django.get_version())
1.6.1
I am using the IDLE (Python GUI).
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
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
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
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 --version
django-admin --version or django-admin.py version
./manage.py --version or python manage.py --version
pip freeze | grep Django
python -c "import django; print(django.get_version())"
python manage.py runserver --version
Django Shell Commands
import django
django.get_version() OR
django.VERSION
from django.utils import version
version.get_version() OR version.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.)
Simply type python -m django --version or type pip freeze to see all the versions of installed modules including Django.
django-admin --version
python manage.py --version
pip freeze | grep django
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
Type in your CMD or terminal:
python -m django --version
Official Documentation
First:
python -m django --version
Second:
import django
print(django.get_version())
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....
Run pip list in a Linux terminal and find Django and its version in the list:
Run pip freeze on cmd on Windows.
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.
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)
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)
After django 1.0 you can just do this
$ django-admin --version
1.11.10
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
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
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”.
Type the following command in Python shell
import django
django.get_version()
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)
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
Open your CMD or Terminal and run any of the following commands
django-admin --version
or
python3 -m django --version
or
pip freeze
From your code, you can get the version of Django by using any of the two below.
import django
print(django.__version__)
# '3.1.5'
print(django.VERSION)
# (3, 1, 5, 'final', 0)
or from your terminal, you can run
django-admin --version
It's very simple open the CLI(command line or any IDE) wherever you installed python and Django just type,
django-admin --version
see here I have installed the latest Python and Django in my system and the result is shown in fig.
There are two more methods to get the Version (of Django and other packages).
Both of them need a version variable for the package to get the version.
According to PEP-396 the __version__variable should be set for every Python module.
Method 1 - Get version from filesystem
With that in mind, you know how to get the version for almost every Django/Python package. Look inside the __init__.py of the package root.
So if you are a fast at navigating through the filesystem, this can be a very universal way of getting the Version of any package inside your site-package (virtual environment).
Method 2 - Django Debug Toolbar
There is a very helpful tool that is called django debug toolbar.
If you use it (very recommendable for Django development) you can list the versions of all apps that have a package.__version__.
Related
I want to see the django version in my Pycharm terminal, but I don't get the correct method.
I tried bellow methods in pycharm terminal:
1) django --version and django version
2) import django, and print the version by:
import django
print django.VERSION
But I still can not get it.
If you cannot print the Django version from the python console in Pycharm, go to settings>Project:project_name>project Interpreter and from the list of installed packages see the installed Django and it's version for that project.
You can run pip freeze too. Just filtering the results with grep...
pip freeze | grep Django
You're trying to access a version attribute, but you can find out Django's version using the get_version method:
import django
django.get_version()
In the terminal you can use bellow command to check the version of django:
python -m django --version
If you want to use your second method, just like this bellow command:
python -c "import django; print(django.VERSION)"
(1, 11, 2, u'final', 0)
go to setting > project > project Interpreter and install Django package
then run the code
import django
print(django.get_version())
Run pip freeze > requirements.txt
A .txt file with that name will be created automatically, open the file and check for the django version and other previously installed libraries.
I do not know about pychram but the most efficient way to query the django version without importing django globally would be this:
from django import VERSION as DJANGO_VERSION
if DJANGO_VERSION >= (2, 0):
pass
What was not proposed in this question which is something similar:
How to check Django version
I had the same question. The solution is simple.
Just create a project, open settings.py file and observe the first comment lines. You will see the version info in there as displayed below:
Image of settings.py that shows the Django version
I found the solution here.
Simplest is
import django
print(django.get_version())
Using the following command on terminal will give you django version
python -m django --version
Or
You can go to the interactive python prompt and use the command
import django
print(django.get_version())
or using pip freeze and grep you can also get the django version
pip freeze | grep Django
I am trying to install OpenCV on my Ubuntu system. I followed the steps of this page. I am able to run the demos for C/C++ etc. But when I try to run the python demos, I get an ImportError: No module named cv2
Here is more info :
python --version
Python 2.7.8 :: Anaconda 2.1.0 (64-bit)
python -c 'import sys; print sys.path'
['', '/home/radar/anaconda/lib/python2.7/site-packages/pydy-0.2.1-py2.7.egg', '/home/radar', '/home/radar/opencv-2.4.9/modules/python/src2', '/home/radar/anaconda/lib/python27.zip', '/home/radar/anaconda/lib/python2.7', '/home/radar/anaconda/lib/python2.7/plat-linux2', '/home/radar/anaconda/lib/python2.7/lib-tk', '/home/radar/anaconda/lib/python2.7/lib-old', '/home/radar/anaconda/lib/python2.7/lib-dynload', '/home/radar/anaconda/lib/python2.7/site-packages', '/home/radar/anaconda/lib/python2.7/site-packages/PIL', '/home/radar/anaconda/lib/python2.7/site-packages/Sphinx-1.2.3-py2.7.egg', '/home/radar/anaconda/lib/python2.7/site-packages/runipy-0.1.1-py2.7.egg', '/home/radar/anaconda/lib/python2.7/site-packages/setuptools-5.8-py2.7.egg']
You are running Anaconda Python distribution, but you need to use native python to access openCV library. Run Python with specified path and it should work:
/usr/bin/python
Also note, that to use openCV with virtual environmet, you need to manually copy openCV running objects (cv.py, cv2.so) to the environment you are going to use from the dist-packages of your native python, default path is /usr/local/lib/python2.7/dist-packages/.
I have uninstalled python 2.5 and installed 2.7 and 3.4 on my Mac OS X 10.9.2.
Somehow when i try to install a new module, i am getting the following error
Thomass-MacBook-Pro:dev thomas$ pip install lxml
-bash: /usr/local/bin/pip: /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python: bad interpreter: No such file or directory
I googled and tried things for hours, played around with symlinks but somehow the system still thinks that 2.5 is the standard version.
Check what Python version are using pip command
head -1 /usr/local/bin/pip | sed s/#\!//g
In my OSX, the python is:
/usr/bin/python
Then, check that python version:
/usr/bin/python -V
If you get 2.5 then something was wrong with your python runtime upgrade, replace the first line in /usr/local/bin/pip by the python2.7 executable. This is not elegant, but It should work temporary until you get clear your system
This sounds a bit odd, but in googling for my similar error* , I found this post and decided to try restarting my machine. That actually made everything start working, which makes me think there was a caching issue. I see this issue opened was about a week ago for you, so hopefully you're in better shape now.
* virtualenv-2.7 thought that was associated with a recently uninstalled python3.4, but virtualenv correctly was associated with 2.7
first of all i'm working on osx 10.6.8
i installed scrapy using the command:
sudo easy_install Scrapy
and everything seemed working fine. but when i try to create a new project using
scrapy startproject tutorial
i only get
Scrapy 0.22.2 requires Python 2.7
i tried this solution, but i get the same problem.
how could i fix this?
EDIT: i think i have understood the problem: easy-install referrs to an older version of python (2.6) that it is in my system under /Library/Python/2.6 (probably it was pre-installed), so i installed scrapy for python 2.6! that's why i get the error. but now i don't know how to install scrapy for the right version of python!
Execute
which python
and
python --version
my guess is scrapy is not linking to python2.7 by default.
Please, open /Library/Frameworks/Python.framework/Versions/2.7/site-packages/scrapy/__init__.py and insert at line 14:
13 if sys.version_info < (2, 7):
14 print(sys.version_info)
15 print("Scrapy %s requires Python 2.7" % __version__)
16 sys.exit(1)
then scrapy startproject tutorial and show us the output
i solved following [this][1] guide and executing again:
sudo easy_install scrapy
now it works!
I've just recently switched over to using 64-bit Python 2.6.1 on Mac OS X 10.6 (Snow Leopard). IPython won't work with Django anymore, but IPython works from the command-line.
The error says:
shell = IPython.Shell.IPShell(argv=[])
AttributeError: 'module' object has no attribute 'Shell'
I could use the ./manage.py --plain option, but it's not really a fix. Any help very gratefully received!
IPython 0.11 has a different API, for which a fix exists in the last Django versions.
For older Django versions, you can use IPython 0.10, which does work:
pip install ipython==0.10
Does this work when you run it from Python interactive console?
import IPython; IPython.Shell
I got the same problem, and solved this issue by applying the patches for the Django bug. - IPython doesn't find the Shell.IPShell class