Setting up Virtual Environment
A virtual environment (venv) isolates project dependencies.
This prevents situations like:
- Project A needs Flask 2.x
- Project B needs Flask 3.x
- both break if you install packages globally
Recommended: venv (built-in)
Section titled “Recommended: venv (built-in)”Pick a project folder (example: my-flask-app/), then create a venv:
python3 -m venv .venvActivate it:
- Linux/macOS:
source .venv/bin/activateYou’ll usually see your shell prompt change.
Upgrade pip (optional but recommended)
Section titled “Upgrade pip (optional but recommended)”python -m pip install --upgrade pipInstall packages into the venv
Section titled “Install packages into the venv”When the venv is activated, pip install ... installs inside that environment.
Freeze dependencies (good habit)
Section titled “Freeze dependencies (good habit)”pip freeze > requirements.txtLater, someone can reproduce your env:
pip install -r requirements.txtCommon errors
Section titled “Common errors”- You forgot to activate the venv →
pipinstalls globally. - You activated a different venv in another terminal.
- Your editor uses a different interpreter than the terminal.
If you’re using VS Code, select the Python interpreter pointing to .venv.
What a virtual environment actually changes
Section titled “What a virtual environment actually changes”A virtual environment is not a sandbox and not a container. It is a directory plus one
redirected value: sys.prefix. Everything else follows from that.
flowchart TD
P["python running"] --> Q{"sys.prefix == sys.base_prefix?"}
Q -->|"equal"| G["system interpreter
pip installs go system-wide"]
Q -->|"different"| V["inside a venv
pip installs go to the venv"]
V --> SP["site-packages resolved
from sys.prefix/Lib/site-packages"]
G --> SG["site-packages resolved
from the system install"]
import sys
sys.prefix # ...\scratch\flaskenv <- the venv
sys.base_prefix # ...\Python\pythoncore-3.14-64 <- the real install
sys.prefix != sys.base_prefix # True -> you are in a venvThat comparison is the reliable test. Checking the VIRTUAL_ENV environment variable is
not reliable: it is set by the activate script, so running
venv/Scripts/python.exe directly puts you in the venv with VIRTUAL_ENV unset —
measured exactly that way here.
See it move
Section titled “See it move”Check yourself
Section titled “Check yourself”-
What is the reliable way to tell, from inside Python, whether you are in a virtual environment?
VIRTUAL_ENV is set by the activate script only. Running venv/Scripts/python.exe directly puts you in the venv with that variable unset — measured exactly that way. sys.prefix != sys.base_prefix is the real test.
pch.quizShowAnswer
B — compare sys.prefix with sys.base_prefix; they differ inside a venv — VIRTUAL_ENV is set by the activate script only. Running venv/Scripts/python.exe directly puts you in the venv with that variable unset — measured exactly that way. sys.prefix != sys.base_prefix is the real test.
-
What does the activate script actually do?
Activation is convenience only. Isolation comes from which interpreter runs, which is why venv/bin/python -m pip install works with no activation and CI scripts rarely activate.
pch.quizShowAnswer
B — it edits PATH and the shell prompt, so that 'python' resolves to the venv binary — Activation is convenience only. Isolation comes from which interpreter runs, which is why venv/bin/python -m pip install works with no activation and CI scripts rarely activate.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading