You want to install the Django framework to develop an application or web site.
There are various ways to get the Django framework. You can download Django from its main site https://www.djangoproject.com/download/ and install it like a regular Python application (e.g.
python setup.py install). You can also download and install Django via an operating system(OS) package administration tool such as
apt-get -- available on Linux distributions like Debian and Ubuntu. Or you can also download and install Django via a Python package administration tool like
The list of Django installation options including their pros and cons is presented in Table 1.
Download/install from Python package manager (pip)
|Allows install on virtual Python environment|
Dependencies are taken care of automatically
|Latest version may not be available|
|Download from main site (tar.gz)||Easiest access to latest stable release||Requires manual download and install |
Requires getting other dependencies
|Download from Git ||Access to the latest features||Can contain bugs|
Requires getting other dependencies
|Download/install from OS package manager (apt-get)||Easy to install|
Dependencies are taken care of automatically
|Latest version may not be available|
Installed on global Python environment
As illustrated in table 1, the recommended option to install Django is to use a Python package manager because it provides the most flexibility. Next, I'll describe each of the steps to install Django with this approach. Once I finish these steps, I'll also describe the steps to install Django from Git, which can be helpful if you want to try out the latest Django features.
Since Django runs on Python, you first need to install Python. Django 1.11 requires at a minimum Python 2.7 or higher. In addition, Django 1.11 can also run on Python 3.5 or higher, but see the sidenote if you will be using the Python 3 branch.
|Django and Python 3|
Django uses Six (http://pythonhosted.org/six/) to run Python 2 and Python 3 compatible logic. Six is a set of utilities that wraps over differences between Python 2 and Python 3. Even if you're experienced with Python 3 (e.g. know the main differences between writing code for Python 3.x vs. Python 2.x) I recommend you read the following Django/Python 3 specific document which recommends a series of practices which Django itself uses, that help keep your Python application compatiable between Python 2 and Python 3 https://docs.djangoproject.com/en/1.11/topics/python3/ .
If you use a Unix/Linux OS, Python may already be installed on your system. If you type
which python on a Unix/Linux terminal and it returns a response (e.g.
/usr/bin/python) it indicates the location of the Python executable, if there is no response it indicates the Python executable is not available on the system. If you don't have Python on your system and you're using a Debian or Ubuntu Linux distribution, you can use the OS package manager
apt-get to install Python by typing:
apt-get install python. If you have a Unix/Linux distribution that is not Debian or Ubuntu and you need to install Python, consult your Unix/Linux documentation for available Python packages or download the Python sources from http://python.org/download/ to do the installation.
If you have a system that runs on a Windows OS or Mac OS, Python installers are available for download from http://python.org/download/.
Irrespective of your system's OS, once you've finished the Python installation, ensure Python is installed correctly and accessible from anywhere on your system. Open a terminal and type
python, you should enter a Python interactive session like the one illustrated in listing 1.
If you aren't able to enter a Python interactive session, review the Python installation process because you will not be able to continue with the following sections.
To make Python package installation and management easier, Python uses a package manager called pip. If you're using Python 2.7.9 (or greater 2.x branch) or Python 3.4 (or a greater 3.x branch), pip comes insalled by default. Next, let's upgrade pip on your system as illustrated in listing 2.
As you can see in listing 2, to update pip you invoke the
pip executable with the arguments
install --upgrade pip. Upon execution,
pip searches for a package by the provided name -- in this case pip itself -- downloads it and performs an upgrade in case it's already installed. If the installation output on your system is similar to the one in listing 2 -- without any errors -- you have successfully updated
If you see an error like
The program 'pip' is currently not installed or
pip not found, it means your Python installation is not equipped with pip. In this case, you'll need to install the
pip executable by downloading https://bootstrap.pypa.io/get-pip.py and then executing the downloaded file with the command:
python get-pip.py. Once the
pip executable is installed, run the pip update procedure from listing 2.
pipon your system, you're ready to move on to the next step.
Virtualenv is not essential to develop Django applications, but I highly recommend you use it because it allows you to create virtual Python environments on a single system. By using virtual Python environments, applications can run in their own 'sandbox' in isolation of other Python applications. Initially virtualenv can appear to be of little benefit, but it can be of tremendous help for tasks like replicating a development environment to a production environment and avoiding version conflicts that can arise between different applications.
Without virtualenv you can still proceed to install Django and any other Python package using
pip, but the issue is all packages are installed under the global Python installation. Initially this can seem convenient, because you only need to install packages once in the global Python installation. But it's not that convenient if you think about some of the following questions.
What happens if a new Django version is released after your first project and you want to start a second project ? Do you upgrade the first project to run on the new Django version or start the second project as if the new Django version doesn't exist ? The first option requires additional work, while the second option requires you to develop on an outdated Django version. By using virtual Python environments you avoid this problem, because each project can run its own Django version in isolation.
If you consider this potential version conflict for any Python package, you'll realize why I recommend you use virtualenv. Many Python packages have specific version dependencies (e.g. Package A depends on package B version 2.3 and package C version 1.5). If you update a new package with specific cross-dependency versions, it can be very easy to break a Python installation if you're using a global Python installation. With virtualenv you can have multiple Python installations without them interfering with one another.
Now that I've explained the benefits of virtualenv, lets install the
virtualenv executable with
pip, as show in listing 3.
As illustrated in listing 3,
pip automatically downloads and installs the requested package. Similar to the
pip package executable, a
virtualenv executable is also installed that should be accessible from anywhere on your system. The
virtualenv executable allows you to create virtual Python environments. Listing 4 illustrates how to create a virtual Python environment with
virtualenv executable accepts several parameters. The task in listing 4 makes use of the parameter
--no-site-packages, which tells virtualenv to create a virtual Python environment without any of the packages already installed in the global Python installation. This creates a virtual Python environment like the one you had when you made the initial Python global installation. Following the
virtualenv parameters, you only need to specify an argument for the name of the virtual Python environment, which in the case of listing 4 is
mydjangosandbox. Upon execution,
virtualenv creates a directory with the virtual Python environment whose contents are illustrated in listing 5.
As illustrated in listing 5, the virtual Python environment has a similar directory structure to the global Python installation. The
bin directory contains executables for the virtual environment, the
include directory is linked to the global Python installation header files, the
lib directory is a copy of the global Python installation libraries and where packages for the virtual environment are installed, the
local directory is linked to top level directories of the virtual directory to simulate a Python installation and the
share directory is used to place shared Python packages.
The most important part of the virtual environment are the executables under the
bin directory. If you use any of these executables, such as
wheel they execute under the context of the virtual Python environment. For example, the
bin installs packages for the virtual environment. Similarly, an application that runs on the
python executable under
bin is only able to load packages installed on the virtual Python environment. This is the 'sandbox' behavior I mentioned previously.
Even though access to different virtual Python environments and executables is a powerful feature, having different
python executables for multiple virtual Python environments and the global Python installation can become confusing due to long access paths and relative paths. For this reason,
virtualenv has a mechanism to load virtual environments so that if you execute
python or any other executable from anywhere on your system, the executables from a selected virtual environments are used (instead of the default global Python installation executables). This is achieved with the
activate executable inside the
bin directory, a process illustrated in listing 6.
Notice in listing 6 how after invoking the
activate executable, the command prompt adds the virtual environment name between parenthesis. This means the executables under the
bin directory of the virtual Python environment
mydjangosandbox are used over those in the global Python installation. To exit a virtual Python environment just type
deactivate and you fallback to using the global Python installation executables.
As you've now learned,
virtualenv works transparently allowing you to maintain different Python installations each with its own set of executables like the main
python interpreter and the
pip package manager. You only need to take care of switching between virtual environments so you install and run Python applications in the appropriate virtual environment.
In future sections I won't make many references to
virtualenv since it's isn't directly related to Django. Though I recommend you use it, I'll leave it up to you if you want to keep using the global Python installation
pip executables for everything or if you prefer to keep virtual Python environments with their own executables to make Python application management easier.
Once you have all the previous tools working on your system, the actual Django installation is very simple. Listing 7 illustrates how to install Django using
pip install task in listing 7 uses a syntax to install a specific package version. The syntax
pip to download and install the Django 1.11 version. With this same syntax you can install an older Django version. If you don't specify a package version,
pip downloads and installs the most recent available version for the specified package.
Sometimes a Django release may take a few days to become available through
pip, in which case you'll receive an error. In such cases you can download the release directly from the Django main site at https://www.djangoproject.com/download/. Once you download the release file in
tar.gz format, you can use
pip to make the installation as illustrated in listing 8.
Notice in listing 8 how
pip is capable of installing Python packages directly from a compressed file on the local file system.
If you want to use the most recent functionality in Django, then you'll need to install Django from its Git repository. The Git repository contains the latest changes made to Django. Even though the Django Git version can be unstable, it's the only way to develop with the newest Django features or get bug-fixes for problems that aren't yet available in public releases.
|You need to install Git|
You need to install Git to execute the following tasks. You can download Git for several OS at http://git-scm.com/
Just like the prior
pip installation examples,
pip is sufficiently flexible to make a Django installation from Git. There are two alternatives to use
pip with Git. You can provide the remote Django Git repository, in which case
pip clones the repository locally and discards it after the installation, as illustrated listing 9. Or you can clone the Django Git repository locally -- where you'll be able to make modifications at a later time -- and then run
pip to do the installation, as illustrated in listing 10.
Notice in listing 9 the syntax to download a remote Git repository is
git+ followed by the remote Git location. In this case
https://github.com/django/django.git represents the Django Git repository. In listing 10 the Django Git repository is cloned locally first and then
pip is executed with the argument of the local Git repository directory.