06.3 - Virtual Environments, pip & Dependency Management
Why Virtual Environments?
Without virtual environments, all projects share the same global Python installation — creating dependency conflicts:
Project A needs requests==2.28
Project B needs requests==2.31
→ CONFLICT!
A virtual environment gives each project its own isolated Python and packages.
Creating and Using venv
# Create
python3 -m venv venv
# Activate (macOS/Linux)
source venv/bin/activate
# Activate (Windows PowerShell)
venv\Scripts\Activate.ps1
# Prompt changes to show (venv)
(venv) $ pip install requests
# Deactivate
deactivate
Always add venv/ to .gitignore:
# .gitignore
venv/
__pycache__/
*.pyc
.env
pip — Package Management
# Install
pip install requests
pip install requests==2.31.0 # specific version
pip install "requests>=2.28" # minimum version
# Upgrade
pip install --upgrade requests
# Uninstall
pip uninstall requests
# List installed packages
pip list
pip list --outdated
# Show package info
pip show requests
# Search (deprecated — use pypi.org instead)
requirements.txt
# Generate from current environment
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt
requirements.txt example:
requests==2.31.0
flask==3.0.0
pandas>=2.0.0
numpy>=1.24.0
python-dotenv==1.0.0
pip Best Practices
| Development | Production | |
|---|---|---|
| File | requirements-dev.txt | requirements.txt |
| Versions | Flexible (>=) | Pinned (==) |
| Includes | Testing, linting tools | Runtime only |
uv — Modern Fast Package Manager
uv is a blazing-fast replacement for pip, developed by Astral:
# Install uv
pip install uv
# Create and manage virtual environments
uv venv
uv pip install requests
# Sync from requirements
uv pip sync requirements.txt
# Install with pyproject.toml
uv install
Key Vocabulary
| Term | Definition |
|---|---|
| Virtual environment | Isolated Python installation per project |
venv | Built-in module to create virtual environments |
| pip | Python package installer |
requirements.txt | File listing project dependencies |
pip freeze | Outputs all installed packages with pinned versions |
uv | Ultra-fast modern Python package manager |
| Dependency conflict | Two packages requiring incompatible versions of a shared dependency |
Summary
- Always create a virtual environment per project:
python3 -m venv venv - Activate before installing anything:
source venv/bin/activate - Use
pip install packageto install;pip freeze > requirements.txtto save - Add
venv/to.gitignore— never commit the virtual environment - Pin versions in production (
==), use flexible versions in development (>=) - Consider
uvfor faster dependency management
📄️ 06.1 - Modules & Imports
Organize code with Python modules, understand import mechanics, absolute vs relative imports, and the __all__ convention
📄️ 06.2 - Creating Packages
Build a Python package with __init__.py, submodules, and a proper directory structure
📄️ 06.3 - Virtual Environments & pip
Isolate project dependencies with venv, manage packages with pip, and use requirements.txt and pyproject.toml
📄️ Lab - Module 06
Create a properly structured Python package with modules, __init__.py, and a virtual environment
📄️ Quiz - Module 06
30 questions on modules, packages, imports, virtual environments, and pip