Installing Django

Install Django into a virtual environment with pip.

Syntaxpip install django

Django is a Python package, so you install it with pip. The recommended workflow is to create a virtual environment first so each project keeps its own dependencies.

Steps

  1. Make sure Python 3.10+ is installed (python --version).
  2. Create and activate a virtual environment.
  3. Install Django with pip.
  4. Confirm the version.

Example

Example · bash
# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate        # Windows: venv\Scripts\activate

# Install Django
pip install django

# Check it worked
python -m django --version

When to use it

  • A developer sets up a virtual environment and installs Django before starting a new freelance project.
  • A DevOps engineer pins Django to a specific version in requirements.txt to ensure reproducible production builds.
  • A tutorial instructor verifies the Django version installed on student machines before the workshop begins.

More examples

Install Django in virtualenv

Creates an isolated virtual environment then installs the latest stable Django into it.

Example · bash
python -m venv venv
source venv/bin/activate
pip install django

Pin version in requirements.txt

Installs a specific Django version range and saves all dependencies to requirements.txt.

Example · bash
pip install "django>=4.2,<5.0"
pip freeze > requirements.txt

Verify installed Django version

Two ways to confirm which Django version is active in the current environment.

Example · bash
python -m django --version
python -c "import django; print(django.__version__)"

Discussion

  • Be the first to comment on this lesson.