Warming up the neural circuits...
By the end of this chapter you will:
Two projects, two incompatible versions, one Python. Project A needs requests==2.28, Project B needs requests==2.31. Without isolation, installing one breaks the other. Virtual environments solve this by giving each project its own Python installation.
# Create virtual environment
python -m venv venv
# Activate (macOS/Linux)
source venv/bin/activate
# Activate (Windows)
venv\Scripts\activate
# Deactivate
deactivate
# Verify
which python # should point to venv/bin/python| Command | Effect |
|---|---|
python -m venv venv | Create isolated Python |
source venv/bin/activate | Activate (macOS/Linux) |
venv\Scripts\activate | Activate (Windows) |
deactivate | Return to system Python |
# Install
pip install requests
# Specific version
pip install requests==2.28.0
# Version specifiers
pip install "requests>=2.28,<3.0"
# Upgrade
pip install --upgrade requests
# Uninstall
pip uninstall requests
| Command | Effect |
|---|---|
pip install pkg | Install latest |
pip install pkg==1.0 | Install exact version |
pip install "pkg>=1.0,<2.0" | Install range |
pip install --upgrade pkg | Upgrade |
pip uninstall pkg | Remove |
pip list | List all |
pip show pkg | Package details |
# Save current environment
pip freeze > requirements.txt
# Recreate environment
pip install -r requirements.txt
# requirements.txt format
requests==2.28.0
numpy==1.24.0
pandas==2.0.0Best practices:
==) for reproducibility>= for minimum versions in librariesrequirements.txt to version controlvenv/ folder# BAD: Global install
pip install requests # installs globally
# Project A: requests==2.28
# Project B: requests==2.31
# Installing one breaks the other!
# GOOD: Virtual environment
python -m venv venv
source venv/bin/activate
pip install requests # installs in venv onlyWhy global is bad:
Think of virtual environments as separate Python installations:
python and pipvenv/lib/python3.x/site-packages/PATH to use the venv Python| Mistake | Why it's wrong | Fix |
|---|---|---|
pip install without venv | Installs globally | Always activate venv first |
Committing venv/ to git | Huge, machine-specific | Add venv/ to .gitignore |
| Forgetting to activate | Installs in wrong environment | Check which python |
pip freeze without venv | Captures system packages | Always run in activated venv |
"requests>=2.28,<3.0" mean?pip install requests without a virtual environment bad?Beginner:
"What is a virtual environment?"
An isolated Python installation for a specific project. Each project gets its own set of packages, so Project A can use
requests==2.28while Project B usesrequests==2.31without conflicts.
"How do you create and activate a virtual environment?"
Create:
python -m venv venv. Activate:source venv/bin/activate(macOS/Linux) orvenv\Scripts\activate(Windows). Deactivate:deactivate.
Senior:
"What's the difference between pip install and pip install -r requirements.txt?"
pip install packageinstalls one package.installs all packages listed in the file. The file is created with and ensures reproducible environments.
python -m venv venv.pip install package installs. pip freeze > requirements.txt saves.pip install -r requirements.txt recreates the environment.I want to…
├── Create venv → python -m venv venv
├── Activate → source venv/bin/activate
├── Install → pip install package
├── Save deps → pip freeze > requirements.txt
├── Recreate → pip install -r requirements.txt
└── Deactivate → deactivateHow do you create a virtual environment?
pip freeze > requirements.txt"Why might you use uv instead of pip?"
uvis a faster package installer written in Rust. It's 10-100x faster than pip for installing packages. It also has better dependency resolution and supports lockfiles. It's becoming the standard for Python projects.