Installing Django

To create a new Django application you must have the following things installed on your computer:

  1. Python.
  2. A Virtual Environment.
  3. Django.

Installing Python on Windows

In this tutorial, we will use Python 3.4. Let’s start by installing Python on Windows first.

Note: Throughout this course instructions are given for Windows, Ubuntu, and Mac. Most of the commands will work no matter which OS you are using. However, there are some commands which vary from one system to another. If that’s the case, I have clearly mentioned it and provided commands specific to the system.

Windows users can download the Python setup from https://www.python.org/ftp/python/3.4.4/python-3.4.4.msi.

After downloading the installer double click to open it. Select “Install for all users” and click Next.

installing python step 1

In the next window, the installer will ask you to select destination directory, keep it to default (i.e C:\Python34) and click next.

installing python step 2

This step is the most crucial. In this step, the installer will ask you to customize the Python installation. Scroll down a little and select “Add python.exe to PATH”, click the dropdown icon in front of it and select “Will be installed on local hard drive”. This allows us to invoke python.exe in the Command Prompt from anywhere in the file system without specifying it’s full path.

installing python step 3

installing python step 3-2

Click Next. At this point, you may get a confirmation window like the following, asking you to confirm the installation. Select Yes to proceed.

installing python step 4

The installer will take a minute or two, depending upon the speed of your system to complete the installation.

installing python step 5

Once done. Click Finish to exit the installer. Now you should have Python 3.4 installed on your PC.

To verify the Python installation execute the following command in the Command Prompt.

Python Package Manager

In Python, we use pip (Python Package Index) to install and manage different packages (or libraries) available at https://pypi.python.org/pypi . It is important to note that pip itself is a package and is used to install other packages. The packages you install using pip will be installed in the directory C:/python34/lib/site-packages. Windows installer automatically installs pip by default, so you don’t need to do anything else. To check the version of pip installed on your system execute the following command.

As you can see, this system has pip 7.1 and is installed under Python 3.4.

Installing Python on Linux

On a Linux distribution like Ubuntu or Fedora, it is highly likely that Python 3.4 or above is installed. To check, open terminal and type the following command.

As you can see, my Ubuntu machine has Python 3.5 installed by default. Although the whole tutorial is geared towards Python 3.4, it would be perfectly fine if you choose to use Python 3.5. We will be using Django 1.10 in this course which only works with Python 3.4 and 3.5. So make sure you have either Python 3.4 or 3.5 installed on your system. Trying to use Django 1.10 with some other version of Python may result in unexpected issues.

If you want to work with Python 3.4 instead of Python 3.5 in Ubuntu type the following commands in the terminal.

Execute each of these commands one by one on the command line. These commands first download the Python from ppa:fkrull/deadsnakes repository and then installs it on your system. On a slow connection the whole process may take some time, so please be patient. To invoke Python 3.4 type python3.4 instead of python3 in the command line as follows:

To exit the Python shell hit Ctrl+D or type quit() in the shell.

Python installer for Ubuntu doesn’t install pip. To install it execute the following command:

To verify the pip installation execute the following command.

Notice that pip3 is installed as a package which belongs to Python 3.5, not Python 3.4.

Installing Python on Mac

Most Mac comes with Python 2.7 installed by default. To install Python 3.4 on a Mac visit https://www.python.org/downloads/release/python-344/ and scroll down to bottom of the page, under Files select the installer corresponding to your version of Mac OS.

python installer for mac

If you are using 32-bit version of Mac OS X or later download Mac OS X 32-bit i386/PPC installer and if you are using 64-bit version of Mac OS X or later download Mac OS X 64-bit/32-bit installer. On a 64-bit system, both installers will work.

After downloading the installer, double-click to start it and go through the installation process as usual.

Python installer for Mac OS also installs pip so you don’t need to install anything else. To verify Python and pip installation execute the following commands in the terminal.

Notice that in Ubuntu and Mac, we are checking the version of pip using pip3 --version instead of pip --version command as we did in Windows. This is because Linux/Mac usually comes pre-installed with Python 2 and most of the time pip --version command is associated with pip installed under Python 2.

Installing Python Virtual Environment

Create a new directory named TGDB (short for “The Great Django Blog”) using the mkdir command.

We will use this folder to store our Django application. You can create this directory anywhere, location doesn’t really matter. I am using Windows and I have created this directory in C:\Users\Q where Q is my username. Once done, change your current working directory to TGDB using the cd command, as follows:

Now we are ready to install Virtual Environment.

So what is this Virtual Environment?

A virtual environment helps us to run isolated instances of Python/Django projects on a machine without conflicting with one another. To understand the philosophy behind Virtual Environment, consider the following example:

Let’s say we are working on two projects, a blog and a forum for two different clients. Our blog uses a library called super_library_v02, on the other hand, our forum uses super_library_v01. At a given point in time, we can only have a single version of super_library installed on our system, we can’t have both versions simultaneously. A Virtual Environment helps us to tackle these kinds of problems easily.

A Virtual Environment solves this problem by creating a separate Python installation. So, no matter what libraries you install on a particular virtual environment using pip, will not conflict with the libraries available at the system-wide Python installation.

The package required to create these isolated environments is called virtualenv.

To install virtualenv on Windows open command prompt and type the following command.

To install virtualenv in Ubuntu/Mac type the following command.

To create virtual environment type virtualenv command followed by the name of the virtual environment. Here is how you can create a virtual environment in Windows:

By default, virtualenv creates virtual environment using the version of Python under which it is installed. In the case of Windows, it is Python 3.4 whereas in Ubuntu it is Python 3.5.

To specify any other version of the Python use -p option as follows:

The above command will create a virtual environment using Python 2.7. Before specifying the version of the python using the -p option make sure that the version of the Python specified is installed on your computer.

We can use the same command as Windows to create a virtual environment in Ubuntu/Mac:

In Ubuntu, the above command will create a virtual environment using Python 3.5 because virtualenv was installed as a package of Python 3.5.

In case you want to follow along using Python 3.4 in Ubuntu, delete the env directory created by virtualenv command and then create a new virtual environment using Python 3.4, as follows:

To know the absolute path of Python 3.4 use the which command.

So what actually virtualenv command does?

The virtualenv command creates an isolated environment, a directory which we named env for developing applications using Django/Python. Once virtualenv finished setting up a new virtual environment, open Windows Explorer or Nautilus in Ubuntu to view the files and folders virtualenv has created for us inside the env directory.

In Windows, the contents of env directory should look like this:

files and directories created by virtualenv

In Ubuntu/Mac, the contents of env directory should look like this:

So what these files and folder contain?

These files and folder constitute a separate python installation. Any libraries or packages you install here will be available only inside this virtual environment, so you can work on your project without conflicting with other packages installed on the system.

Activating virtualenv

We have created a virtual environment in the last step, to use it we first have to activate it.

Activating virtualenv in windows

To activate virtual environment in Windows type the following command.

Activating virtualenv in Ubuntu/Mac

On Linux/Mac, we use source command to activate virtual environment.

Notice (env) in front of the prompt string, it indicates that your virtual environment is up and running. From this point on, any package you add or remove using pip will only affect this virtual environment. Your system-wide Python installation will remain intact. We can use pip list command to view packages installed in this virtual environment.

This virtual environment has 3 packages installed. It is important to note that once the virtual environment is active you can invoke pip either using pip or pip3. This is true for Window, Ubuntu as well as Mac.

To deactivate virtual environment issue the following command.

This command is same for Windows, Ubuntu, and Mac. Now we are out of the virtual environment. Run pip list command again, but this time it will show you all the system-wide packages installed on your system.

Note: ... indicates that the code snippet is truncated to save space.

On Ubuntu/Mac, you should use pip3 list to view system-wide packages installed on the system.

In Ubuntu:

In Mac:

So these packages are available on my system-wide Python installation. Yours could be different.

In the next step, we will install Django.

Installing Django

In this tutorial, we will use Django 1.10. To install Django open terminal and type the following command.

This command fetches the Django framework from PyPI and installs it into your virtual environment. The output of the command should look like this:

If you just want to install current stable version simply issue the following command.

Testing the Installation

Inside the virtual environment start the Python shell by typing python command.

If you are on Ubuntu/Mac start the Python shell using python3 (or python3.4) command.

To verify whether the installation was successful or not, import django package and call get_version() method as follows.

If everything went fine you should get the version of the Django installed. If you have encountered any error go through all the above steps or post a comment in the box below and we will figure out the problem.

To exit the Python shell type Ctrl + Z (in Windows), Ctrl + D (in Linux/Mac) or just type quit().

2 thoughts on “Installing Django

  1. Using module vertualenv to create venv looks more clear and straightforward to me. At least I know that all dependencies will be settled later in a Lib folder . It’s not so direct if we use conda to create and activate venv this way.

Leave a Comment