Chapter 2 · 10 min read
Setup: Python, venv and Django
Setup: Python, venv and Django
Before you write any Django code you need three things: a recent Python, a virtual environment to keep your project's packages separate, and Django itself. This chapter walks through each one and explains why it matters.
What you will learn
- How to check that your Python is new enough for Django 6.1.
- What a virtual environment is, and how to create and activate one with
venv. - How to install Django 6.1.3 with
pipand confirm the version. - How to keep unwanted files out of Git with a
.gitignorefile.
Why a virtual environment?
Python packages normally install into one shared location. If project A needs Django 5.2 and project B needs Django 6.1, they would fight over the same folder. A virtual environment is a private folder that holds its own copy of Python's packages for one project. When it is active, python and pip use only that folder.
| Without a virtual environment | With a virtual environment |
|---|---|
| All projects share one set of packages | Each project has its own packages |
| Upgrading a package can break another project | Upgrades affect only one project |
| Hard to know what a project needs | pip freeze lists exactly what the project needs |
Which Python do I need?
Django 6.1 supports Python 3.12, 3.13, 3.14 and 3.15. Python 3.13 or 3.14 is a good choice. Older Python versions cannot install Django 6.1: pip will report that no matching version exists, because the package requires Python 3.12 or newer.
If you can only use Python 3.10 or 3.11, install Django 5.2 LTS instead, which supports Python 3.10 to 3.14:
python -m pip install "Django>=5.2,<5.3"
Step 1: check your Python version
macOS and Linux:
python3 --version
Windows (the py launcher is installed with Python):
py --version
The output should read Python 3.12.x or higher. If it is older, download the latest Python from https://www.python.org/downloads/. On Windows, tick the box Add python.exe to PATH in the installer. On macOS you can also use Homebrew with brew install python. On Linux, if your distribution ships an older Python, install a newer one from python.org, with a tool such as uv, or with pyenv.
Step 2: create a project folder and a virtual environment
macOS and Linux:
mkdir myblog
cd myblog
python3 -m venv .venv
source .venv/bin/activate
Windows:
mkdir myblog
cd myblog
py -m venv .venv
.venv\Scripts\activate
Here is what each line does:
mkdir myblogmakes a new folder, andcd myblogmoves into it.python3 -m venv .venvruns Python's built-invenvmodule and creates a folder named.venvcontaining a private Python.- The
activatecommand changes your terminal so thatpythonandpippoint at the private copy.
After activation your prompt starts with (.venv). That prefix is how you know the environment is active. If you open a new terminal window later, you must activate it again.
On Windows PowerShell, activation may be blocked by the execution policy. Either run this once, then activate again:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
or use Command Prompt (cmd) instead of PowerShell.
Step 3: install Django
Inside the active environment the command python is the same on every system:
python -m pip install --upgrade pip
python -m pip install "Django==6.1.3"
python -m django --version
The last command should print 6.1.3. Writing python -m pip instead of plain pip guarantees that you use the pip belonging to the Python you are running.
Pinning the version with ==6.1.3 makes your setup repeatable. You can also write "Django>=6.1,<6.2" to accept future 6.1 patch releases automatically. Patch releases such as 6.1.3 are compatible with the feature release and contain bug and security fixes, so upgrading patch releases is always recommended.
Step 4: set up Git ignore rules
Some files should never be committed: the virtual environment, compiled Python files, your local database and collected static files. Create a file named .gitignore in myblog:
.venv/
__pycache__/
db.sqlite3
staticfiles/
Then start a repository:
git init
Check it works
python -m django --versionprints6.1.3.- Your terminal prompt shows
(.venv)at the start. python -c "import django; print(django.__file__)"prints a path that contains.venv.
Exercises with solutions
Exercise 1: see what the environment does
Run deactivate, then run python -m django --version. Read the error. Activate the environment again and run the command once more.
Solution. Without the environment active, Python cannot find Django and reports No module named django (or, if you installed Django globally, a different version). After you activate the environment, the command prints 6.1.3 again. This shows that Django lives inside .venv, not on your whole computer.
Exercise 2: record your dependencies
Create a file that lists the exact packages in your environment.
Solution.
python -m pip freeze > requirements.txt
Open requirements.txt. You should see Django==6.1.3 and its helper packages such as asgiref and sqlparse. Someone else can recreate your environment with python -m pip install -r requirements.txt.
Exercise 3: a second environment
Create another environment called .venv2, install Django 5.2 LTS into it, and confirm the version. Then delete it.
Solution.
python3 -m venv .venv2
source .venv2/bin/activate
python -m pip install "Django>=5.2,<5.3"
python -m django --version
deactivate
On Windows use py -m venv .venv2 and .venv2\Scripts\activate. The version printed starts with 5.2. Delete the folder afterwards. Re-activate .venv before continuing the course.
Common mistakes
- Forgetting to activate the environment in a new terminal. Symptom:
No module named django. Fix: run the activate command again. - Using an old Python. Symptom: pip says there is no matching distribution for Django 6.1. Fix: install Python 3.12 or newer, or use Django 5.2 LTS.
- Committing
.venvto Git. The folder is large and machine-specific. List it in.gitignorebefore your first commit. - Mixing up activation commands. macOS and Linux use
source .venv/bin/activate. Windows uses.venv\Scripts\activate. - Installing with a different pip. If
pipandpythonbelong to different installations, packages end up in the wrong place. Always usepython -m pip.
Key takeaways
- A virtual environment keeps one project's packages separate from every other project.
- Django 6.1 needs Python 3.12 or newer; Django 5.2 LTS works on older Pythons.
python -m pip install "Django==6.1.3"thenpython -m django --versionis the whole installation.- Keep
.venv/anddb.sqlite3out of Git.
Official links
- Quick install guide: https://docs.djangoproject.com/en/6.1/intro/install/
- Python versions FAQ: https://docs.djangoproject.com/en/6.1/faq/install/
- Python venv tutorial: https://docs.python.org/3/tutorial/venv.html
