How to use virtual environments

Why isolate dependencies

Different projects may require different package versions. A virtual environment gives a project its own Python executable and installed packages without changing the system Python.

mkdir weather-app
cd weather-app
python3 -m venv .venv

Activate the environment

Activation places the environment's executables first on PATH. The prompt often shows (.venv).

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

# Windows Command Prompt:
.venv\Scripts\activate.bat

# Windows PowerShell:
.venv\Scripts\Activate.ps1

Install and verify

After activation, installs go into the environment. Confirm which interpreter is active before installing.

python --version
python -m pip install requests
python -m pip show requests

Leave and recreate

Run deactivate to leave. Do not commit .venv; record dependencies and recreate the environment instead.

python -m pip freeze > requirements.txt
deactivate

# Later:
python3 -m venv .venv
# activate, then:
python -m pip install -r requirements.txt