Ad – 728Γ—90
πŸ”§ Intermediate

Python Virtual Environments – Isolate Project Dependencies

A virtual environment is an isolated Python environment with its own interpreter and package library. Without one, all your projects share the same global Python installation β€” meaning a version conflict in one project can break others. Virtual environments solve this completely.

⏱️ 16 min read🎯 IntermediateπŸ“… Updated 2026

Why Virtual Environments Matter

Without virtual environments: Project A needs requests 2.0, Project B needs requests 3.0 β€” they cannot coexist in global pip. Virtual environments give each project its own isolated space.

Python
# Without venv (global):
# pip install django==3.2  β†’ breaks Project B which needs django==4.2

# With venv:
# project_a/venv/ β†’ django 3.2 installed here
# project_b/venv/ β†’ django 4.2 installed here
# Zero conflict!

Creating a Virtual Environment

Use Python's built-in venv module. Create one per project.

Python
# Create virtual environment (from project root)
python -m venv venv         # Windows
python3 -m venv venv        # macOS/Linux

# This creates:
# venv/
#   bin/python (or Scripts\python.exe on Windows)
#   bin/pip
#   lib/           ← installed packages go here

Activating and Deactivating

Always activate before working on a project.

Python
# Activate:
# Windows (Command Prompt):
venv\Scripts\activate

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

# macOS / Linux:
source venv/bin/activate

# Prompt changes to show (venv) prefix
# (venv) $ pip install requests   ← installs ONLY in this env

# Deactivate when done:
deactivate
πŸ’‘
Tip

Your prompt shows (venv) when a virtual environment is active. If you don't see it, you're installing into the global Python.

Ad – 336Γ—280

Installing and Managing Packages

Install packages with pip after activating your venv.

Python
# Activate first, then:
pip install requests flask
pip install pandas==2.0.0    # specific version

# See what's installed:
pip list
pip show requests            # details about one package

# Save your dependencies:
pip freeze > requirements.txt

# Reproduce on another machine:
pip install -r requirements.txt

Never Commit the venv Folder

Add venv/ to .gitignore. Teammates recreate it from requirements.txt.

Python
# .gitignore
venv/
__pycache__/
*.pyc
.env
*.egg-info/
dist/
build/

# Teammate setup:
git clone your-repo
cd your-repo
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt