Skip to content

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

Pick a project folder (example: my-flask-app/my-flask-app/), then create a venv:

python3 -m venv .venv
python3 -m venv .venv

Activate it:

  • Linux/macOS:
source .venv/bin/activate
source .venv/bin/activate

Youโ€™ll usually see your shell prompt change.

python -m pip install --upgrade pip
python -m pip install --upgrade pip

Install packages into the venv

When the venv is activated, pip install ...pip install ... installs inside that environment.

Freeze dependencies (good habit)

pip freeze > requirements.txt
pip freeze > requirements.txt

Later, someone can reproduce your env:

pip install -r requirements.txt
pip install -r requirements.txt

Common errors

  • You forgot to activate the venv โ†’ pippip installs 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 coffee

Was this page helpful?

Let us know how we did