It is important to start correctly by installing Django, Python 3, and virtual environments in a reusable manner. By the end of this short chapter, we will be able to start new Django projects in just a few keystrokes.

Note that we use Python 3 exclusively in this book. Future versions of Django will not support Python 3 and development of Python 2.7 will stop after 2020. In short, in 2017 there is no reason to still be using Python 2 for new Django projects. For a more detailed overview see Appendix A: Python 2 vs Python 3.

The Command Line

The command line is a powerful, text-only view of your computer that we will use extensively throughout this book.

On a Mac computer, it can be accessed via a program called Terminal that is located in the /Applications/Utilities folder.

On a Windows computer, I recommend downloading Babun rather than the built-in command line program, which is quite difficult to use.

Within a new command line console try typing echo "hello, world" and hit Return as your first command. It should display the text "hello, world" as output on the ensuing line.

$ echo "hello, world"
"hello, world"

NOTE: The $ dollar sign is our command line prompt. All commands in this book are intended to be typed after the $ prompt.

If you’re brand new to the command line, you can find a brief overview and links to further learning resources in Appendix B: The Command Line.

Install Python 3 on Mac OS X (click here for Windows)

Although Python 2 is installed by default on Mac computers, Python 3 is not. You can confirm this by typing python --version in the command line console and hitting Enter:

$ python --version
Python 2.7.13

To check if Python 3 is already installed try running the same command useing python3 instead of python.

$ python3 --version

If your computer outputs 3.6.x (any version of 3.6 or higher) then skip ahead to creating a virtual environment. However most likely you’ll see an error message since we need to install Python 3 directly.

Our first step is to install Apple’s Xcode package, so run the following command to install it:

$ xcode-select --install

Click through all the confirmation commands (Xcode is a large program so this might take a while to install depending on your internet connection).

Next, install the package manager Homebrew via the longish command below:

$ /usr/bin/ruby -e "$(curl -fsSL"

To confirm Homebrew installed correctly, run this command:

$ brew doctor
Your system is ready to brew.

And now to install the latest version of Python, run the following command:

$ brew install python3

Now let’s confirm which version was installed:

$ python3 -V
Python 3.6.2

To open a Python 3 interactive shell–this lets us run Python commands directly on our computer–simply type python3 from the command line:

$ python3
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

To exit the Python 3 interactive shell at any time type Control-d (the “Control” and “d” key at the same time).

You can still run Python shells with Python 2 by simply typing python:

$ python
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Install Python 3 on Windows

Python is not included by default on Windows, however we can check if any version exists on the system. Open a command-line console by entering command on the Start Menu. Or you can hold down the SHIFT key and right-click on your desktop, then select Open Command Window Here.

Type the following command and hit RETURN:

python --version
Python 3.6.2

If you see output like this, Python is already installed. Most likely it will not be!

To download Python 3, go to the downloads section of the official Python website. Download the installer and make sure to click the Add Python to PATH option, which will let use use python directly from the command-line. Otherwise we’d have to enter our system’s full path and modify our environment variables manually.

After Python has installed, run the following command in a new command-line console:

python --version
Python 3.6.2

If it works, you’re done!

Create a Virtual Environment

Virtual environments are a way to create an isolated space for individual Python projects. You should use them for all Python work that you do. We will create new virtual environments for every new Django project going forward in a two-step process: first creating a new one and then activating it.

It’s a best practice to keep all your virtualenvs in one place, for example .virtualenvs/ in your home directory. Let’s create that directory:

$ mkdir ~/.virtualenvs

Now create a new virtual environment called myenv by running:

$ python3 -m venv ~/.virtualenvs/myenv

And then activate it with the following command:

$ source ~/.virtualenvs/myenv/bin/activate

The result will be future command line prompts have the name of your virtual environment in parentheses before the rest of the prompt. The command above results in the following:

(myenv) $

Because we used python3 when creating this environment, the default for Python is now Python 3. You can confirm this by typing python --version and it will show a Python 3 result. However outside of a virtual environment, python will result in Python 2 and python3 results in Python 3.

To stop using a virtual environment, either close the command-line console or enter deactivate:

(myenv) $ deactivate

You can also fully delete a virtual environment by using the rmvirtualenv command after you’ve closed it with the deactivate command. Do so with the command below.

$ rmvirtualenv myenv

The myenv virtual environment is now fully removed from the computer.

Install Django

Now let’s install Django with within a virtual environment with pip, Python’s package manager. We just deleted our myenv environment but we can quickly recreate it as follows:

$ python3 -m venv ~/.virtualenvs/myenv
$ source ~/.virtualenvs/myenv/bin/activate
(myenv) $

Now install Django:

(myenv) $ pip install django
Collecting django
Installing collected packages: pytz, django
Successfully installed django-1.11.3 pytz-2017.2

Note: If you receive a permissions error, you may need add sudo to the command for it to work: sudo pip install django.

Django 1.11 requires the Python Timezone package, pytz, so pip installs it in your virtual environment, too. We can see all installed software packages within our virtual environment by using the command pip freeze:

(myenv) $ pip freeze

Congratulations! For future Django projects, all you need to do is create and activate a new virtual environment, install a fresh version of Django, and away you go!


In this chapter we learned about the command line and used it to install Python 3, Django, and create a new virtual environment. In the next chapter we’ll create our first Django app.

Continue on to Chapter 2: Hello World app.

Sign up for the Django For Beginners newsletter for updates when new chapters are available and special discounts for the print edition of the book.