← Back to Chapters

Installing Python and Django

? Installing Python and Django

? Quick Overview

To start working with Django, you first need to install Python and Django. This guide walks you through setting up Python, pip, Django, and virtual environments for a smooth Django development experience.

? Key Concepts

  • Python is the core language used by Django
  • pip is used to install Python packages
  • Django can be installed globally or inside virtual environments
  • Virtual environments isolate project dependencies

? Installing Python

? View Code Example
# Python installation on Ubuntu Linux
sudo apt-get update
sudo apt-get install python3
# Check installed Python version
python3 --version

If you're using Windows or macOS, download the latest Python version from the official Python website.

? Installing pip (Python Package Installer)

? View Code Example
# Install pip for Python 3
sudo apt-get install python3-pip
# Verify pip installation
pip3 --version

pip is the tool used to install Python packages, including Django. Make sure it is installed correctly.

? Installing Django

? View Code Example
# Install Django using pip
pip3 install django
# Verify Django installation
django-admin --version

This installs the latest version of Django globally. You can also install specific versions if required.

? Installing Django in a Virtual Environment

It is a best practice to install Django inside a virtual environment to avoid dependency conflicts.

? View Code Example
# Install virtualenv package
pip3 install virtualenv
# Create virtual environment
python3 -m venv myenv
# Activate virtual environment
source myenv/bin/activate
# Install Django inside virtual environment
pip install django
# Verify Django version
django-admin --version

? Explanation

  • Installing Python: Python is required for Django and version 3.6 or later is recommended.
  • Installing pip: pip manages Python packages including Django.
  • Installing Django: Django can be installed globally or per project.
  • Virtual Environments: They keep dependencies isolated per project.

? Interactive Example

Below is a simple command flow showing how Django setup typically looks:

? View Code Example
# Typical Django project creation flow
django-admin startproject myproject
cd myproject
python manage.py runserver

? Interactive Terminal Sim

Click a command to see the result:

$ _

? Use Cases

  • Building web applications using Django
  • Creating APIs with Django REST Framework
  • Developing scalable backend systems
  • Learning full-stack Python development

✅ Tips & Best Practices

  • Always use a virtual environment for Django projects
  • Use Python 3.6 or later for best compatibility
  • Create a new virtual environment for each project

? Try It Yourself

  • Install Python and Django on your system
  • Create and activate a virtual environment
  • Run django-admin startproject and start the server