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:

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

Activate it:

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

You’ll usually see your shell prompt change.

bash
python -m pip install --upgrade pip
bash
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)

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

Later, someone can reproduce your env:

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

Common errors

  • You forgot to activate the venvpippip 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