We’ll use Django Version 1.5. To get started we need to do a little preparation.
Django is written completely in Python. Therefore Python needs to be installed first.
Note
Django 1.5 supports Python from version 2.6.5. It’s recommended to use Python version 2.7.3 or higher. If you have an older version of Python, you should update it.
Since version 1.5, Django has experimental support for Python 3.2 and later. Django 1.6. will have stable support for Python 3.
You can find out which version of Python you’re running by using the command line option -V:
$ python -V
Python 2.7.4
Note
If you are using Python 3 please make sure you have Python 3.3.2 or greater installed. Otherwise there will be problems.
Also consider adding the following future-import on top of every Python file you are going to edit to ensure Python 2 and 3 compatibility:
from __future__ import unicode_literals
This way all regular strings will be unicode string literals.
If you want to learn more read the Python 3 part of the Django documentation.
If you’ve already got the right version of Python installed, you can skip ahead to Python Package Managers.
Many Linux distributions come with Python already installed. If you haven’t got a version of Python installed, you can normally use your package manager to download and install it.
Alternatively, you can get the Python Sources from the website and compile it yourself.
Python comes pre-installed on Mac OS X. You can however use Homebrew to install your own copy of Python.
Download the Installer from the Python Website and install it.
So that Python works under Windows as expected, you need to change the environment variable %PATH%. In the examples, we’ll assume that your Python is installed in C:\Python27\.
Python uses its own package system to manage distribution and installation of Python packages. Because we will need to install several packages, we must first install the package manager.
First setuptools needs to be installed. Some systems install it by default, this can be verfied by executing:
$ python -c "import setuptools"
If the execution results in an ImportError setuptools is not installed and you have to follow the commands below. Otherwise continue with the installation of pip.
It’s installed with the help of a bootstrap script which can be downloaded here. If installed, you can use curl to download it at the command line. Otherwise just use the browser.
$ curl -O https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
When the bootstrap script has been downloaded execute it to install setuptools:
$ python ez_setup.py
Note
Under Linux and Mac OS X root privileges may be required.
You can delete the bootstrap script when the installation has been finished.
We will use pip to install the packages. pip was originally written as an improvement of easy_install. pip can be installed with the help from a bootstrap script which can be downloaded from GitHub. If installed, you can use curl to download it at the command line. Otherwise just use the browser.
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
When the bootstrap script has been downloaded execute it to install pip:
$ python get-pip.py
Note
Under Linux and Mac OS X root privileges may be required.
You can delete the bootstrap script when the installation has been finished.
After installation, you can test pip as follows:
$ pip --version
pip 1.4.1 from /home/docs/lib/python2.7/site-packages (python 2.7)
As soon as you work with more than one project you will sooner or later have collisions between Python packages. Maybe an old project still needs an older version of a package while you want to use the latest version for your new project. This is where virtualenv can help.
virtualenv provides a “container” for each of your projects. Each virtualenv can be separated from the system Python installation and from other virtualens. Furthermore each virtualenv can be associated to a different Python version. Finally virtualens can be used in production to separate different projects on a single host.
Install virtualenv using pip:
$ pip install virtualenv
Note
Under Linux and Mac OS X root privileges may be required.
After the installation create a directory where you will create all your virtualenvs, for example in your home directory:
$ mkdir .virtualenvs
Note
If you are using Windows use Envs instead of .virtualens.
virtualenvwrapper makes the creation and every day work with virtualenvs much easier by providing a lot of additional helpers.
Install virtualenvwrapper using pip:
$ pip install virtualenvwrapper
Note
Under Linux and Mac OS X root privileges may be required.
After the installation add the following two lines to your .bashrc or .profile:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
WORKON_HOME defines where all virtualenvs are located. The script virtualenvwrapper.sh loads the helpers.
Reload your shell configuration to be able to use virtualenvwrapper:
$ source .bashrc
Windows users can install virtualenvwrapper-win instead of virtualenvwrapper:
$ pip install virtualenvwrapper-win
Note
virtualenvwrapper-win does not work with PowerShell, use the Command Prompt (cmd.exe) instead.