Django Environment and Installation

Introduction

Before you create a project or run manage.py, you need Python 3.10+, an isolated virtual environment, and Django 5 installed into that environment—not globally on the system Python. This chapter verifies Python, creates a venv, installs Django, sets up an editor, and prepares a .gitignore and requirements.txt so later chapters start from a reproducible baseline.

Prerequisites

Version Requirements

ComponentMinimumRecommended
Python3.103.11 or 3.12
Django5.0Latest 5.x from PyPI
pipRecentUpgrade with python -m pip install -U pip

Django 5.x supports the Python versions listed on the official download page. Use 3.10+ to match this track and the Python tutorials.

Tip

Recommended Combo

Python 3.12 + Django 5.x + venv per project is a solid learning setup on Windows, macOS, and Linux.

Step 1: Verify Python

bash
python --version
python -m pip --version

On some systems use python3 instead of python:

bash
python3 --version
python3 -m pip --version

Expected: Python 3.10.x or newer.

Windows if python is missing:

powershell
py -3 --version

Code explanation:

  • python -m pip ensures pip runs for the same interpreter you will use for Django
  • If multiple Python versions exist, always activate venv before installing Django (next step)

Warning

Do Not Install Django Globally on Shared Machines

Use a virtual environment per project so Django and dependency versions do not conflict with other Python work.

Step 2: Create a Workspace Folder

bash
mkdir hello-django
cd hello-django

This directory will hold the venv and (in the next chapter) your Django project files.

Step 3: Create and Activate a Virtual Environment

Windows (PowerShell or CMD)

bat
python -m venv .venv
.venv\Scripts\activate

Prompt should show (.venv) at the start of the line.

macOS / Linux

bash
python3 -m venv .venv
source .venv/bin/activate

Deactivate anytime:

bash
deactivate

Code explanation:

  • .venv/ lives inside the project folder (name is convention; .venv is common)
  • After activation, python and pip point inside .venv/
  • Re-run activate in every new terminal session before working on the project

More detail: pip and venv patterns.

Step 4: Upgrade pip and Install Django

With venv activated:

bash
python -m pip install -U pip
python -m pip install "Django>=5.0,<6"

Pin for teams (optional, after install):

bash
python -m pip freeze | findstr Django    # Windows
python -m pip freeze | grep Django     # macOS/Linux

Example line in requirements.txt:

text
Django==5.1.7

Create initial requirements file:

bash
python -m pip freeze > requirements.txt

Code explanation:

  • Django>=5.0,<6 keeps you on Django 5.x without accidentally pulling Django 6 when it exists
  • requirements.txt lets others run pip install -r requirements.txt for the same versions

Step 5: Verify Django Installation

bash
python -m django --version
python -c "import django; print(django.get_version())"

Expected: 5.0.x or 5.1.x (or newer 5.x).

Quick sanity check that management commands exist:

bash
python -m django help

You should see subcommands like startproject, runserver, migrate (used starting in the next chapter).

Editor Setup

VS Code

  1. Open the hello-django folder
  2. Install extensions: Python (Microsoft), Django (optional syntax/templates)
  3. Python: Select Interpreter → choose .venv/Scripts/python.exe (Windows) or .venv/bin/python (macOS/Linux)

PyCharm

  1. Open → select hello-django
  2. Settings → Project → Python Interpreter → Add → Existing → point to .venv
  3. PyCharm Professional has built-in Django support; Community works fine with the Python plugin

Code explanation:

  • Wrong interpreter = packages installed in venv but IDE runs system Python → import errors
  • After changing interpreter, restart the terminal inside the IDE if imports fail

.gitignore for Django Projects

Create hello-django/.gitignore (project root, next to .venv):

See also Git .gitignore.

Warning

Never Commit .env or Production Secrets

Use .env for SECRET_KEY and database URLs in later chapters—keep it out of Git.

Optional: django-extensions (Later)

Not required now. Some teams install:

bash
python -m pip install django-extensions

Provides shell_plus, runserver_plus, etc. This track mentions it only as optional tooling.

Optional: uv or pip-tools

Hello Code documents pip and uv. For Django learning, venv + pip is enough. Switch to uv when your team standardizes on it—the venv workflow stays the same.

Common Install Problems

SymptomLikely causeFix
python: command not foundPython not installed or not on PATHReinstall Python; use py -3 on Windows
No module named djangovenv not activated or Django installed elsewhereactivate then pip install Django again
pip installs to wrong placeWrong Pythonwhich python / where python should show .venv
Permission denied on LinuxInstalling without venv as userUse venv; avoid sudo pip install
SSL / network errorsProxy or firewallConfigure pip index mirror or corporate proxy
Django 4 installed by mistakeOld requirements pinpip install "Django>=5.0,<6"

Verify which Python pip uses:

bash
python -c "import sys; print(sys.executable)"

Path should include .venv.

Post-Install Checklist

  • python --version shows 3.10+
  • .venv created and activated ((.venv) in prompt)
  • python -m pip install "Django>=5.0,<6" succeeded
  • python -m django --version prints 5.x
  • IDE interpreter set to .venv
  • .gitignore and requirements.txt in project root

FAQ

python vs python3?

On macOS/Linux, use whichever maps to 3.10+ after venv activation—usually both work inside .venv. Outside venv, python3 is often explicit.

Do I need Docker for learning?

No. Local venv is enough until the Docker deploy chapter.

Can I use Conda instead of venv?

Yes. Create a Conda env with Python 3.10+, then pip install Django. See Conda in Python.

WSL2 on Windows?

Create venv inside WSL (Linux steps). Run Django on 127.0.0.1:8000; browse from Windows at the same address.

Install Django in PyCharm wizard?

Fine for quick tests; this track uses terminal + venv so commands match production and CI docs.

Why pin Django>=5.0,<6?

Keeps tutorials aligned with Django 5 APIs. Adjust upper bound when your team upgrades major versions deliberately.