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:
python3 -m venv .venvpython3 -m venv .venvActivate it:
- Linux/macOS:
source .venv/bin/activatesource .venv/bin/activateYouโll usually see your shell prompt change.
Upgrade pip (optional but recommended)
python -m pip install --upgrade pippython -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)
pip freeze > requirements.txtpip freeze > requirements.txtLater, someone can reproduce your env:
pip install -r requirements.txtpip 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
