01 / 20

Virtual Environments
venv

Learn why Python projects use isolated environments to manage dependencies, prevent conflicts, and keep your system clean.

Python

What Is a Virtual Environment?

A virtual environment is a self-contained directory that holds its own Python interpreter and its own set of installed packages: completely independent from your system-wide Python installation.

Why Do We Need Them?

System Python requests 2.25 requests 2.31 flask 1.1 flask 2.3 ✕ Version Conflict! vs. Project A venv requests 2.25 flask 1.1 Project B venv requests 2.31 flask 2.3 ✓ No Conflict! Without venv With venv

The Dependency Problem

Every Python package can depend on other packages, each at specific versions. When two projects require different versions of the same package, a conflict arises:

1
Project A needs pandas 1.5 for legacy data processing.
2
Project B needs pandas 2.1 for the latest features.
3
Python can only keep one version installed globally: installing one breaks the other.

Anatomy of a venv

my_project/ .venv/ bin/ (or Scripts/) python, pip, activate lib/ site-packages/ requests/ flask/ pyvenv.cfg config metadata Each venv contains its own Python binary, pip, and installed packages

The Golden Rule of venv

Isolation
Packages installed inside a venv are invisible to code running outside it.
Reproducibility
The exact set of packages and versions can be shared via a requirements file.
Cleanliness
Your system Python stays minimal and uncluttered by project-specific packages.

How Isolation Works

How "activate" changes your PATH Before Activation PATH: /usr/bin/python3 /usr/lib/python3/... pip install goes to global site-packages After Activation PATH: .venv/bin/python3 /usr/bin/python3 pip install goes to .venv/lib/site-packages

venv vs. Other Approaches

venv virtualenv conda
Built-in? ✓ Yes (Python 3.3+) ✕ Separate install ✕ Separate install
Speed Fast Very fast Slower
Non-Python deps ✕ ✕ ✓ (C libs, etc.)
Best for Most projects Legacy Python Data science

How to Create and Use a venv

1
Create the environment in your project folder.
2
Activate it to redirect python and pip.
3
Install packages: they go into the venv only.
4
Freeze your dependencies to a requirements file.
5
Deactivate when done working on the project.

Example: Creating a venv

# Step 1: Create the virtual environment $ python3 -m venv .venv # Step 2: Activate it # macOS / Linux: $ source .venv/bin/activate # Windows: > .venv\Scripts\activate # Your prompt now shows the venv name: (.venv) $

Example: Installing Packages

# With the venv activated, pip installs locally (.venv) $ pip install requests flask # Verify: packages live inside the venv (.venv) $ pip list # Package Version # ---------- ------- # flask 3.0.0 # requests 2.31.0 # ... # Check: system Python has NONE of these (.venv) $ deactivate $ pip list # flask and requests are NOT here

Example: Freezing Dependencies

# Save exact package versions to a file (.venv) $ pip freeze > requirements.txt # requirements.txt now contains: # flask==3.0.0 # requests==2.31.0 # jinja2==3.1.2 # ... # A teammate can recreate the EXACT environment: $ python3 -m venv .venv $ source .venv/bin/activate (.venv) $ pip install -r requirements.txt

The Full Workflow

CREATE python -m venv ACTIVATE source activate INSTALL pip install ... DEVELOP write your code FREEZE pip freeze > req COMMIT git add req.txt DEACTIVATE deactivate Never commit the .venv/ folder: commit only requirements.txt .gitignore: add ".venv/" to exclude the venv directory

What Not to Do with .venv

✕ Committing .venv/
The .venv folder can be hundreds of MB. Add it to .gitignore and share only requirements.txt.
✕ Using sudo pip install
Using sudo installs packages globally, defeating the purpose. Always activate first, then just use pip.
✕ Moving the .venv folder
The venv stores absolute paths internally. Moving it breaks everything: delete and recreate instead.

Summary: Key Concepts

Concept Details
Virtual Environment Isolated directory with its own Python + packages
Create Command python3 -m venv .venv
Activate (Unix) source .venv/bin/activate
Activate (Windows) .venv\Scripts\activate
Freeze Deps pip freeze > requirements.txt
Restore Deps pip install -r requirements.txt
Deactivate deactivate
Git Rule Commit requirements.txt, not .venv/

Question 1 of 5

What is the primary purpose of a Python virtual environment?

Question 2 of 5

Which command creates a virtual environment named .venv using the built-in venv module?

Question 3 of 5

What does activating a virtual environment actually do behind the scenes?

Question 4 of 5

Which file should you commit to version control so teammates can recreate your environment?

Question 5 of 5

Why should you never move a .venv directory to a different location after creating it?