Django is fast becoming one of the top choices for web application frameworks. It’s based on a much better language than PHP, a much less-weird language than Ruby and a lighter-weight environment than Java. Unfortunately Python suffers from a horrendous 2.x to 3.x incompatibility in the language and environment. I took the philosophical decision to not bother learning Python 2.x and do everything in Python 3.x. In theory Django has been Python 3 compatible since Django 1.5. This dogma isn’t without its problems when developing using the Django framework.
These notes relate to using Ubuntu 14.04. Whilst the Ubuntu team are actively trying to kick their nasty Python 2.x habit, they haven’t made it yet for 14.04.
Clearly you’re going to need Python 3 – not installed by default in Ubuntu 14.04.
sudo apt-get install python3
Then, to install Django for use with Python 3 you’re going to need pip, but the Python 3 version:
sudo apt-get install python3-pip
Because you can’t get rid of Python 2.x from Ubuntu 14.04 and because Python 2.x is in the path by default and because of a big mess with updates and upgrades, it’s advisable to set up a virtual Python 3 environment for your Django-in-Python-3 development. Otherwise you’re going to keep picking up Python 2, and even if you do manage to get it using Python 3 today, you may find that after an update tomorrow, it all stops working again.
sudo pip3 install virtualenv
Now, let’s say you want to keep your code in
~/src/djangoProjects, make the directory and change working directory to
~/src (i.e. the directory above):
Now you can activate your vitual Python 3 world, free of Python 2 nastiness:
Test that Python 2 is no longer the king of your virtual environment that you just created and avtivated:
So there’s no need to keep adding ‘3’ at the end of the
python command and at the end of the
pip command, etc. Also, now that you are in a virtual Python environment, you no longer need to be root to do pip things and your pip customisation of your Python 3 virtual environment should be immune to Ubuntu updates. So, to install Django:
pip install Django
Now you can check that you have Django for Python 3 installed (note the python3 command rather than python as presented in the standard Django tutorial).
python -c "import django; print(django.get_version())"
You should get the Django version printed to the console – cooking with gas! Now you can continue with the rest of the official Django tutorial.