Set Up a Virtual Environment
Every web project in this course lives in its own folder with its own virtual environment. A virtual environment (or venv) is a private copy of Python's package area, so the Flask version used by your tiffin app cannot clash with the Django version used by your library project.
In this lesson you will check your Python version, create and activate a venv, install Flask and Django, save the exact versions in requirements.txt, and set up VS Code to use the right Python. You will also meet uv, a fast modern tool that many teams now use for the same job.
Why a virtual environment?
Without a venv, pip install puts packages into one shared place for your whole computer. That causes three problems:
- Two projects need different versions of the same package, and only one can be installed.
- You forget which packages a project needs, so it fails on a friend's laptop or on the server.
- Upgrading a package for one project silently breaks another.
A venv solves all three. It is just a folder (we call it .venv) with its own python and pip. You can delete it and recreate it at any time, which is why it never goes into Git.
Check Python first
Django 6.1 needs Python 3.12 or newer; Flask 3.1 needs 3.9 or newer. Check what you have:
python --versionPython 3.14.8
On Windows, if python opens the Microsoft Store or is not found, install Python from python.org (or with the Python install manager, which gives you the py command) and open a new terminal. On Linux and macOS the command is often python3. See Install Python & Set Up for full steps.
Create and activate a venv
Make a project folder and create the venv inside it. The second venv below is the module name; .venv is the folder name.
mkdir pune-tiffin
cd pune-tiffin
python -m venv .venvNow activate it. Activation changes your terminal so that python and pip point into .venv. The command depends on your shell:
| Shell | Activate command |
|---|---|
| Windows PowerShell | .venv\Scripts\Activate.ps1 |
| Windows Command Prompt | .venv\Scripts\activate.bat |
| Git Bash on Windows | source .venv/Scripts/activate |
| Linux / macOS (bash, zsh) | source .venv/bin/activate |
After activation your prompt starts with (.venv). Type deactivate to leave. You must activate again every time you open a new terminal.
To confirm that Python is really running from the venv, ask it where it lives:
python -c "import sys; print(sys.prefix)"C:\Users\aarav\projects\pune-tiffin\.venv
If the path does not end in .venv, you forgot to activate, and pip would install into the wrong place.
Install Flask and Django
With the venv active, install both frameworks. Using python -m pip instead of plain pip guarantees you use the pip that belongs to the active Python.
python -m pip install flask djangoSuccessfully installed asgiref-3.13.0 blinker-1.9.0 click-8.5.0 django-6.1.2 flask-3.1.3 itsdangerous-2.2.0 jinja2-3.1.6 markupsafe-3.0.4 sqlparse-0.6.0 tzdata-2026.6 werkzeug-3.1.9
Two names became eleven packages. Flask brought Werkzeug (requests, responses and the dev server), Jinja2 (templates), Click (the flask command), itsdangerous (signed cookies) and Blinker (signals). Django brought asgiref (async support) and sqlparse; on Windows it also adds tzdata, because Windows has no built-in time zone database.
Check the versions:
flask --version
python -m django --versionPython 3.14.8 Flask 3.1.3 Werkzeug 3.1.9 6.1.2
In real projects you usually install only one of the two frameworks per venv. We install both here because this course uses both.
Pin versions in requirements.txt
When the project works, save the exact versions so anyone (including your server) can recreate the same environment:
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txtThe first line writes lines such as Django==6.1.2 and Flask==3.1.3 into the file. The second line, run on another machine inside a fresh venv, installs exactly those versions. Commit requirements.txt to Git, and add .venv/ to your .gitignore.
A faster option: uv
uv is a newer tool that creates venvs and installs packages much faster than pip. Install it by following its official guide at https://docs.astral.sh/uv/, then:
uv venv
uv pip install flask djangouv venv creates .venv and uv pip install works like pip. For bigger projects, uv init creates a pyproject.toml, uv add django records the dependency and installs it, and uv run python manage.py runserver runs a command inside the project's environment without activating anything. Both pip and uv are fine for this course; pick one and stay consistent within a project.
Your editor
In VS Code, install the Python extension, open the project folder, press Ctrl+Shift+P and choose Python: Select Interpreter, then pick the one inside .venv. New terminals inside VS Code will then activate the venv for you, and the editor will find Flask and Django for auto-completion.
A clean project folder now looks like this:
pune-tiffin/
.venv/ (never commit this)
.gitignore (contains .venv/)
requirements.txt
app.py (your Flask app, next lesson)Common mistakes
- Installing before activating. Packages go into the global Python and the project cannot find them later. Check for
(.venv)in the prompt. - Committing
.venvto Git. It is large and tied to your computer. Commitrequirements.txtinstead. - Mixing
pipandpythonfrom different places. Usepython -m pipto be sure. - Using a Python that is too old. Django 6.1 will not install on Python 3.11 or older; pip then picks an older Django or fails.
Key points
- A virtual environment gives each project its own packages and versions.
- Create one with
python -m venv .venvand activate it before installing anything. - Install with
python -m pip install flask django; check versions withflask --versionandpython -m django --version. pip freeze > requirements.txtpins versions;pip install -r requirements.txtrecreates them.uvis a fast alternative (uv venv,uv pip install,uv add); keep.venvout of Git.
Exercise
Create a folder called jaipur-library, make a venv in it, activate it, install only Django, and save a requirements.txt. Then delete the .venv folder, create a new venv, and reinstall everything from requirements.txt. Finally, print the Django version to prove it worked.
Show solution
mkdir jaipur-library
cd jaipur-library
python -m venv .venv
source .venv/bin/activate # Windows PowerShell: .venv\Scripts\Activate.ps1
python -m pip install django
python -m pip freeze > requirements.txt
deactivate
rm -rf .venv # Windows PowerShell: Remove-Item -Recurse -Force .venv
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python -m django --version6.1.2
Because requirements.txt pinned Django==6.1.2, the new venv got exactly the same version. This is how your deployment server will rebuild the project later.
Quick Quiz
1. What does activating a virtual environment do?
Activation changes the terminal's PATH so the venv's own Python and pip are used.
2. Which file should you commit to Git?
The venv can be rebuilt anywhere from requirements.txt, so only the list of packages is committed.
3. Why use python -m pip install instead of pip install?
python -m pip runs pip as a module of the exact Python you are using.
4. Which Python version is the minimum for Django 6.1?
Django 6.x supports Python 3.12 and newer.
Learn Django & Flask with a mentor
Free lessons get you started. Our Python Programming training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
