Setting up a Django development environment

This article needs an editorial review. How you can help.

Draft
This page is not complete.

Contact Hamish Willee via chrisdavidmills if you have any questions about this work.

Now that you know what Django is for, we'll show you how to setup and test a development environment on Windows, Linux (Ubuntu), and Mac OS X.

Prerequisites: Know how to open a terminal / command line. Know how to install software packages on your development computer's operating system.
Objective: To have a development environment for Django (1.10) running on your computer.

Overview

Django makes it very easy to set up your own computer so that you can start developing web applications. This section explains what you get with the development environment, and provides an overview of some of your setup and configuration options. The remainder of the article shows the recommended installation for Django on Ubuntu, Mac OS X and Windows, and how you can test the environment.

What is the Django development environment?

The development environment is an installation of Django on your local computer that you can use for developing and testing Django apps prior to deploying them to a production environment.

The main tools that Django itself provides are a set of Python scripts for creating and working with Django projects, along with a simple development webserver that you can use to view Django web applications on your computer's web browser.

There are other peripheral tools, which form part of the developer environment, that we won't be covering here. These include things like a text editor or IDE for editing code, and source control management tools like git for safely managing different versions of your code.

What are the Django setup options?

Django is extremely flexible in terms of how and where it can be installed and configured. Django can be:

  • installed on different operating systems.
  • used with Python 3 and Python 2.
  • installed from source, from the Python Package Index (PyPi) and in many cases from the the host computer's package manager application.
  • configured to use one of several databases, which may also need to be separately installed and configured.
  • run in the main system Python environment or within separate Python virtual environments.

Each of these options requires slightly different configuration and setup. The following subsections explain some of your choices. For the rest of the article we'll show you the recommended setup for a small number of operating systems, and that setup will be assumed throughout the rest of this module.

Other possible installation options are covered in the official Django documentation. We link to the appropriate documents below.

What operating systems are supported?

Django web applications can be run on almost any machine that can run the Python programming language: Windows, Mac OS X, Linux/Unix, Solaris, to name just a few. Almost any computer should have the necessary performance to run Django during development.

In this article we'll provide instructions for: Windows, Mac OS X, Linux/Unix.

What version of Python should you use?

Django runs on top of Python, and can be used with either Python 2 or Python 3 (or both). When selecting a version you should be aware that:

  • Python 2 is a legacy version of the language that is no longer receiving new features but has a truly enormous repository of high quality 3rd party libraries for developers to use (some of which are not available for Python 3).
  • Python 3 is an update of Python 2 that, while similar, is more consistent and easier to use. Python 3 is also the future of Python, and continues to evolve.
  • It is also possible to support both versions using compatibility libraries (e.g. six), although not without additional development effort.

Historically Python 2 was the only realistic choice, because very few 3rd party libraries were available for Python 3. The current trend is that most new and popular packages on the Python Package Index (PyPi) support both versions of Python. While there are still many packages that are only available for Python 2, choosing Python 3 is now a feasible option.

Generally speaking we recommend that you use the latest version of Python 3 unless you're going to be using 3rd party libraries that are only available for Python 2.

This article will explain how to install an environments for both versions of Python. However the rest of this module will only show code snippets written using Python 3 code (the equivalent code for Python 2 would be very similar).

Where can we download Django?

There are three places to download Django:

  • The Python Package Repository (PyPi), using the pip tool. This is the best way to get the latest stable version of Django.
  • Use a version from your computer's package manager. Distributions of Django that are bundled with operating systems offer a familiar installation mechanism. Note however that the packaged version may be quite old, and can only be installed into the system Python environment (which may not be what you want).
  • Install from source. You can get and install the latest bleeding-edge version of Python from source. This is not recommended for beginners, but is needed when you're ready to start contributing back to Django itself.

This article shows how to install Django from PyPi, in order to get the latest stable version.

Which database?

Django supports four main databases (PostgreSQL, MySQL, Oracle and SQLite), and there are community libraries that provide varying levels of support for other popular SQL and NOSQL databases. We recommend that you select the same database for both production and development (although Django abstracts many of the database differences using its Object-Relational Mapper (ORM), there are still potential issues that are better to avoid).

For this article (and most of this module) we will be using the SQLlite database that Django is configured to use by default, and which stores its data in a file. This requires no configuration or setup to use.

The SQLite database is not recommended for production use (and yes, we're ignoring the above advice to use the same database for development and production to simplify these instructions). We'll talk more about setting up databases in a later article.

Installing system-wide or in a Python virtual environment

When you install Python you get a single global environment (set of installed packages) for your Python 2 code and single separate environment for your Python 3 code, which you manage using the pip and pip3 tools respectively. While you can install whatever Python packages you like in these environments, you can only install one particular version at a time. This means that any changes you make to any Python application can potentially affect all others, and that you can only have one Django environment at a time.

A much better alternative is to enable Python virtual environments. Python virtual environments allow you to have a multiple independent environments for testing your Python apps. This means that you can create new apps using the latest version of Django while continuing to maintain older websites that use legacy versions, all on the same machine.

This module will assume you've installed Django into a virtual environment, and we'll show you how below (this is the approach Django recommend for both development and production environments).

Using a virtual environment is also great from a documentation point of view because once an environment is setup the instructions for using different versions of Python and different operating systems become almost exactly the same (for example, in a Python 3 virtual environment you can use default commands python and pip to work with your code, rather than the Python 3-specific commands.

The rest of this article shows how to set up Django using the approach recommended by the Django project. Specifically we'll show you how to setup and install Django into a Python virtual environment (running either Python 2 or 3) using pip, on Ubuntu Linux, Mac OS X, and Windows 10.

Installing Python (and pip)

In order to use DJango you will have to have the correct version(s) of the Python runtime environment on your operating system. You will also need the Python Package Index tool (known as pip or pip3 for the respective associated versions of Python) - this is used to manage (install, update and remove) the Python packages/libraries used by the respective Python environments.

This section briefly explains how you can check what versions are present, and install new versions as needed, for Ubuntu Linux 16.04, Mac OS X, and Windows 10.

Depending on you platform, you may also be able to install Python/pip from the package manager for your distribution or via other mechanisms. For most platforms you can download the required installation files from https://www.python.org/downloads/ and install using the appropriate platform-specific method.

Ubuntu 16.04

Ubuntu Linux includes both Python 2 and Python 3 by default. You can confirm this by running the following commands in the bash terminal:

python -V
 Python 2.7.12
python3 -V
 Python 3.5.2

However the Python Package Index tool you'll need to install packages (including Django) is not available by default. You can install pip for both Python 2 and Python 3 in the bash terminal using:

sudo apt-get install python-pip
sudo apt-get install python3-pip

Mac OS X

Mac OS X "El Capitan" includes Python 2.7 by default (<check pip>). You can confirm this by running the following commands in the bash terminal:

python -V
 Python 2.7.12
python3 -V
 -bash: python3: command not found

You can easily install Python 3 (along with the pip tool from python.org):

  1. Download the required installer(s) from https://www.python.org/downloads/
  2. Locate the file using Finder, and open the file. Following the installation prompts

You can now confirm successful installation by checking for the Python3 version as shown:

python3 -V
 Python 3.5.20

Windows 10

Windows doesn't include Python by default, but you can easily install it along with the pip tool from python.org.

  1. Download the required installer(s) from https://www.python.org/downloads/
  2. Install the version(s) of Python by double clicking on the downloaded file and following the installation prompts

You can then test Python by entering the following text into the command prompt:

python -V 
 Python 2.7.12 

The installer should set up everything you need for the above to work. If however you get a message that Python cannot be found, you may need to add it to your system path.

The python command will launch your "default" python. If you have installed both Python 2 and Python 3 then you can differentiate between them as shown below:

py -2 -V 
 Python 2.7.12 
py -3 -V 
 Python 3.5.2

The Windows installers also incorporate the associated Python package managers (pip and pip3) by default. You can list installed packages for each version of Python as shown

pip list #list Python 2 packages 
pip3 list #list Python 3 packages

This topic has shown how to install the Python.org distribution of Python. There is a list of other distributions here.

 

Using Django on a Python virtual environment

The libraries we'll use for creating our virtual environments are virtualenvwrapper (Linux and Mac OS X) and virtualenvwrapper-win (Windows), which in turn both use the virtualenv tool. The wrapper tools creates a consistent interface for managing interfaces on all platforms. They also make it easy to specify an environment in such a way that we can ignore what version of Python we're working with, and always just refer to pip and python (rather than the pip3 and python3 versions).

Installing the virtual environment software

Ubuntu virtual environment setup

After installing the Python and pip versions you want to use, you can install virtualenvwrapper (and bundling virtualenv) using pip as shown.

sudo pip install virtualenvwrapper

Then add the following lines to the end of your shell startup file (this is a hidden file name .bashrc in your home directory). These set the location where the virtual environments should live, the location of your development project directories, and the location of the script installed with this package:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

Then reload the startup file by making the following call in the terminal:

source ~/.bashrc

At this point you should see a bunch of scripts being run as shown below:

virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/postmkproject
...
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/get_env_details

Now you can create a new virtual environment with the mkvirtualenv command.

Mac OS X virtual environment setup

Setting up virtualenvwrapper on Mac OS X is almost exactly the same as on Ubuntu. 

Install virtualenvwrapper (and bundling virtualenv) using pip as shown.

sudo pip install virtualenvwrapper

Then add the following lines to the end of your shell startup file. These are the same lines as for Ubuntu, but the startup file is the differently named hidden file .bash_profile in your home directory.

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

If you can't find .bash-profile to edit in the finder, you can also open this in the terminal using nano.

The commands look something like this.

cd ~  # Navigate to my home directory
ls -la #List the content of the directory. YOu should see .bash_profile
nano .bash_profile # Open the file in the nano text editor, within the terminal
# Scroll to the end of the file, and copy in the lines above
# Use Ctrl+X to exit nano, Choose Y to save the file.

Then reload the startup file by making the following call in the terminal:

source ~/.bash_profile.

At this point you should see a bunch of scripts being run (the same scripts as for the Ubuntu installation). You should now be able to create a new virtual environment with the mkvirtualenv command.

Windows 10 virtual environment setup

Installing virtualenvwrapper-win is even simpler than setting up virtualenvwrapper because you don't need to configure where the tool stores virtual environment information (there is a default value). All you need to do is call:

pip install virtualenvwrapper-win

Now you can create a new virtual environment with the mkvirtualenv command

Creating a virtual environment

Once you've installed virtualenvwrapper or virtualenvwrapper-win then working with virtual environments is very similar on all platforms.

Now you can create a new virtual environment with the mkvirtualenv command. As this command runs you'll see the environment being set up (what you see is slightly platform specific). When the command completes the new virutal enviroment will be active — you can see this because the start of the prompt will be the name of the environment in brackets (as shown below).

$ mkvirtualenv name_for_new_environment

Running virtualenv with interpreter /usr/bin/python3
...
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/t_env7/bin/get_env_details
(name_for_new_environment) ubuntu@ubuntu:~$ 

The above call will pick up the Python and pip version from your system path. As we're planning on using Python 3 we instead explicitly specify the path to the Python 3 executable.

mkvirtualenv --python='directory/path/to/python/executable/' name_for_new_environment

## Some examples ##

# Ubuntu: Explicit path to Python 3
mkvirtualenv --python=/usr/bin/python3 name_for_new_environment

# Ubuntu: Path to Python 3 inferred using "which" command
mkvirtualenv --python=`which python3` name_for_new_environment   

# Windows: Path to a Python 3 executable for a specific user
mkvirtualenv --python="C:\Users\hamis\AppData\Local\Programs\Python\Python35\python.exe" name_for_new_environment 

# Mac OS X: Explicit path to Python 3
mkvirtualenv --python=/Library/Frameworks/Python.framework/Versions/3.5/bin/python3 name_for_new_environment 

There are platform-specific ways for finding the full path to a particular executable (including the which command shown above). One way that will work to get the path to the Python executable on all platforms is to ask Python itself. So for example, to find where Python 2 or 3 are installed you would first launch the Python interpreter using the platform-specific method

python   # Default Python for your system
python2  # Launch Python 3 interpreter on Ubuntu/Mac
python3  # Launch Python 3 interpreter on Ubuntu/Mac
py -2  # Launch Python 2 interpreter on Windows
py -3  # Launch Python 3 interpreter on Windows

Then enter (copy-paste) the following commands. The print() will display the full path.

import sys
print(sys.executable)

For example, on Ubuntu we see:

$ python3
Python 3.5.2 (default, Jul 5 2016, 12:43:10) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.executable)
/usr/bin/python3

Now you're inside the virtual environment you can install Django and start developing.

From now on in this article (and indeed the module) please assume that any commands are run within a Python virtual environment like the one we set up earlier (other commands can be run anywhere, but there is no need to leave the virtual environment to do so).

Using a virtual environment

There are just a few other useful commands that you should know (there are more in the tool documentation, but these are the ones you'll use regularly):

  • deactivate — Exit out of the current Python virtual environment
  • workon — List available virtual environments
  • workon name_of_environment — Activate the specified Python virtual environment
  • rmvirtualenv name_of_environment — Remove the specified environment.

Installing Django using pip

Once you've created a virtual environment, and called workon to enter it, you can use pip to install Django.

Calling pip inside a Python 3 environment (as created above) is actually calling pip3. That's one of the good things about our setup, it doesn't matter what Python you use, all commands from here on are the same.
pip install django

You can trivially test that Django is installed by running the following command in your virtual environment (this just tests that Python can find the Django module):

python -m django --version
 1.10.10

Testing your installation

The above test works, but it isn't very much fun. A more interesting test is to create a skeleton project and see it working. To do this, first navigate in your command prompt/terminal to where you want to store you Django apps. Create a folder for your test site and navigate into it.

mkdir test_django_installation
cd test_django_installation

You can then create a new skeleton site called "mytestsite" using the django-admin tool as shown. After creating the site you can navigated into the folder where you will find the main script for managing projects, called manage.py.

django-admin startproject mytestsite
cd mytestsite

We can run the development web server from within this folder using manage.py and the runserver command, as shown.

(django_env) ubuntu@ubuntu:~/test_django_installation/mytestsite$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

September 19, 2016 - 23:31:14
Django version 1.10.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Once the server is running you can view the site by navigating to the following URL on your local web browser: http://127.0.0.1:8000/. You should see a site that looks like this:

The home page of the skeleton Django app.

Further reading

Summary

You now have a Django development environment up and running on your computer, and you know how to create new environments using mkvirtualenv, and how start and stop it using workon and deactivate, respectively. 

In the testing section you also briefly saw how we can create a new Django website using django-admin startproject, and run it in your browser using the development web server (python manage.py runserver). In the next article we expand on this process, building a simple but complete web application.

Document Tags and Contributors

 Contributors to this page: hamishwillee
 Last updated by: hamishwillee,