Skip to main content

Django - How to Install

The page explains how to install Django using a Virtual Environment

Django is a popular framework written in Python used to code modern and secure web applications much faster. Install Django is the first step of this journey and during this short tutorial, we will learn how to properly install Django using a Virtual Environment.

Django, How to Install - Tutorial provided by AppSeed.

✅ What is Django

For newcomers, Django is a popular framework built by experienced developers, actively supported by an impressive open-source community. Django comes with batteries included concept and provides modules to handle the database, authentication, built-in security, plus a powerful command-line interface to interact with our app. To start learning Django feel free to access:

Django Framework - New Project Start Page.

✅ Virtual Environments

A virtual environment helps us to keep isolated the dependencies used by different Python projects. A simple use case might be this one: we have installed locally two Python apps that use different versions of a common library, Django for instance. Without using a virtual environment the only choice is to install the dependencies in the global environment and this might affect the stability and behavior of both apps.

A virtual environment acts like a sandbox where we can safely install modules with zero effects in the global environment.

Create a new virtual environment

$ python3 -m venv env
// OR
$ virtualenv env

The above commands have identical effects and will create a new env directory in the current working directory. Once the virtual environment is created, the next step is to run the activation command.

Activate virtual environment

$ # Unix based systems
$ source env/bin/activate
// OR
$ # Windows based systems
$ .\env\Scripts\activate

On successful activation, the terminal gets a prefix as below:

(env) $

✅ Install Django

The recommended way to install Python packages is to use PIP, the official package manager for Python.

(env) $ # Install latest Django version
(env) $ pip install django

The above command will install the latest Django version. To install a specific version, please use the syntax:

(env) $ # Install Django 2.2.10
(env) $ pip install "django==2.2.10"

If all goes well, we can check the version using the Python console:

(env) $ python
(env) >>> import django
(env) >>> django.__version__
(env) '2.2.10'

How to uninstall Django or any other pachage

The removal of a Python package can be done with ease via pip:

(env) $ # This will remove Django
(env) $ pip uninstall django
(env) Found existing installation: Django 2.2.10
(env) Uninstalling Django-2.2.10:
(env) ...
(env) Successfully uninstalled Django-2.2.10

At this point, Django is no longer available in our virtual environment.

✅ Deactivate Virtual Environment

Once the work is finished, to leave the virtual environment we need to type:

(env) $ deactivate

After running deactivate command, the console prefix should be removed by the terminal:

$ # no environment prefix

✅ Resources