mypy
**Python Virtual Environments** are **isolated Python installations that maintain separate sets of packages for each project** — preventing the "dependency hell" where Project A needs pandas 1.5 and Project B needs pandas 2.0, and installing one breaks the other, by creating independent directories with their own Python binary and site-packages, ensuring that every project has exactly the dependencies it needs without conflicts.
**What Are Virtual Environments?**
- **Definition**: Self-contained directory trees that include a Python installation and a separate set of installed packages — so that `pip install` inside a virtual environment doesn't affect the system Python or other projects.
- **The Problem**: Without virtual environments, all Python packages install globally. Project A installs tensorflow==2.10, then Project B installs tensorflow==2.15 (overwriting 2.10), and Project A breaks. This is "dependency hell."
- **The Solution**: Each project gets its own isolated environment. Activating an environment switches your PATH so that python and pip point to the environment's copies, not the system's.
**Virtual Environment Tools**
| Tool | Built-in? | Best For |
|------|----------|----------|
| **venv** | Yes (Python 3.3+) | Standard projects, simplest option |
| **virtualenv** | No (pip install) | More features than venv, faster creation |
| **conda** | No (Anaconda/Miniconda) | Scientific computing, non-Python dependencies (CUDA, MKL) |
| **poetry** | No (pip install) | Dependency resolution + lock files + packaging |
| **pipenv** | No (pip install) | Pipfile + Pipfile.lock workflow |
| **uv** | No (pip install) | Blazing fast Rust-based venv + package management |
**Lifecycle (venv)**
```bash
# 1. Create virtual environment
python3 -m venv myenv
# 2. Activate
source myenv/bin/activate # Linux/Mac
myenv\Scripts\activate.bat # Windows CMD
myenv\Scripts\Activate.ps1 # Windows PowerShell
# 3. Verify (should point to myenv/)
which python
# /path/to/project/myenv/bin/python
# 4. Install packages (isolated to this env)
pip install pandas scikit-learn torch
# 5. Freeze requirements
pip freeze > requirements.txt
# 6. Deactivate (return to system Python)
deactivate
# 7. Reproduce environment elsewhere
python3 -m venv newenv && source newenv/bin/activate
pip install -r requirements.txt
```
**venv vs conda**
| Feature | venv | conda |
|---------|------|-------|
| **Python version** | Uses system Python | Can install any Python version |
| **Non-Python packages** | Cannot install C libraries | Can install CUDA, MKL, FFmpeg |
| **Speed** | Fast creation | Slower (dependency solving) |
| **Disk usage** | Lightweight (~10MB) | Heavier (~200MB+) |
| **Best for** | Web dev, general Python | Data science, ML (scientific stack) |
**Common Issues and Fixes**
| Issue | Cause | Fix |
|-------|-------|-----|
| **Permission denied** on activate | File not executable | `chmod +x myenv/bin/activate` |
| **PowerShell won't activate** | Execution policy restriction | `Set-ExecutionPolicy Unrestricted -Scope Process` |
| **Wrong Python version** | System Python used | Specify: `python3.10 -m venv myenv` |
| **Packages not found** after activation | Forgot to activate | Check `which python` points to venv |
**Python Virtual Environments are the essential foundation of reproducible Python development** — isolating project dependencies to prevent conflicts, enabling reproducible builds through requirements.txt or lock files, and ensuring that every collaborator, CI pipeline, and production server runs the exact same package versions.