Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Django 1.9 on Python 3.5 instead of 2.7

Tags:

python

django

I have Python 2.7 and 3.5 running on OSX 10.10 and also Django 1.9a -- which is support for both Python version. The problem is I want to run Django on Python 3.5 instead of 2.7. On some threads I found suggestions to run it by including the Python version e.g: python3.5 manage.py runserver, but I found this error:

File "manage.py", line 8, in <module> from django.core.management import execute_from_command_line ImportError: No module named 'django'

FYI, I have no problem run Python3.5 on the same machine.

How can I solve this? thank you very much!

like image 320
antonifs Avatar asked Dec 04 '22 02:12

antonifs


2 Answers

Note: when I wrote this, Django 1.9 had not yet been released, and Django 1.8 did not support Python 3.5. Since then, Django 1.9 has been released, and Django 1.8.6+ supports Python 3.5. Therefore you can use the latest 1.8.X or 1.9.X with Python 3.5. Choose Django 1.8.X if you want long term support, or 1.9.X if you want newer features.

The Django docs recommend that you create a virtual environment for the version of Python you wish to use, then install Django using pip.

On OS X, pyvenv is installed along with Python 3.

pyvenv djangodev # create the virtual env.  
source djangodev/bin/activate # activate it
pip install django

This will install the latest stable version of Django, currently 1.8.4. The Django 1.9 alpha has only just been released. If you are new to Django, you might have a smoother ride if you start with Python 3.4 and Django 1.8. It will be straight forward to upgrade later. If you definitely want to install the 1.9 alpha, you can install it using pip with

pip install django==1.9a1
like image 54
Alasdair Avatar answered Dec 06 '22 15:12

Alasdair


There is one simple solution for Your problem without virtualenv or like that. I use Python 2.7.12 and 3.5.2 installed with Homebrew.

$ which python3
/usr/local/bin/python3

To install Django for Python 3:

pip3 install django

To create Django project for Python 3:

python3 /usr/local/lib/python3.5/site-packages/django/bin/django-admin.py startproject mysite

Then You can execute all commands in this way:

python3 manage.py shell
python3 manage.py startapp page
python3 manage.py runserver
...

Or You can edit the manage.py file:

#!/usr/bin/env python <— instead this line (first)
#!/usr/local/bin/python3 <— must be this one

And then You can execute all standard commands like:

./manage.py shell
./manage.py startapp page
./manage.py runserver
...
like image 21
Gerdmalex Avatar answered Dec 06 '22 16:12

Gerdmalex