Python · Stage 8 – Organizing Bigger Programs · Lesson 43 of 50

Virtual Environments

Create an isolated place for one project’s packages.

python -m venv .venv
Line-by-line explanation
  1. Python runs its built-in venv module.
  2. .venv is the folder to create.
  3. That folder receives an isolated interpreter and package location.

Activate it in Windows PowerShell:

.venv\Scripts\Activate.ps1
Line-by-line explanation
  1. The path points to the environment’s PowerShell activation script.
  2. Running it changes the current terminal session.
  3. The prompt normally gains (.venv).

Activate it on macOS or Linux:

source .venv/bin/activate
Line-by-line explanation
  1. source runs changes in the current shell.
  2. The path selects the environment’s activation script.
  3. The prompt normally gains (.venv).

Tiny Practice

Create an environment folder named .venv.

Starter template

python -m venv CHANGE_ME
Line-by-line explanation
  1. The venv command is complete.
  2. Only the destination folder is missing.
Hint
  1. Use the common name .venv.
  2. Do not quote it.
  3. Run inside the project folder.
Show Solution
python -m venv .venv
Line-by-line explanation
  1. Python loads venv.
  2. The module creates an isolated environment.
  3. Its files are placed in .venv.