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: venvvenv (built-in)
Pick a project folder (example: my-flask-app/my-flask-app/), then create a venv:
bash
python3 -m venv .venvbash
python3 -m venv .venvActivate it:
- Linux/macOS:
bash
source .venv/bin/activatebash
source .venv/bin/activateYou’ll usually see your shell prompt change.
Upgrade pip (optional but recommended)
bash
python -m pip install --upgrade pipbash
python -m pip install --upgrade pipInstall packages into the venv
When the venv is activated, pip install ...pip install ... installs inside that environment.
Freeze dependencies (good habit)
bash
pip freeze > requirements.txtbash
pip freeze > requirements.txtLater, someone can reproduce your env:
bash
pip install -r requirements.txtbash
pip install -r requirements.txtCommon errors
- You forgot to activate the venv →
pippipinstalls 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.venv.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
