Don't install packages globally. Use virtual environments.
Why Virtual Environments?
Without them:
- Projects share dependencies
- Version conflicts break things
- "Works on my machine" problems
- Upgrading one project breaks another
With them:
- Each project has its own packages
- No conflicts between projects
- Reproducible environments
- Clean uninstall
Creating a Virtual Environment
# Create
python -m venv venv
# Activate (macOS/Linux)
source venv/bin/activate
# Activate (Windows)
venv\Scripts\activate
# Deactivate
deactivateThe venv folder contains a copy of Python and all installed packages.
Project Structure
myproject/
├── venv/ # Don't commit this
├── src/
├── tests/
├── requirements.txt
├── pyproject.toml
└── .gitignore
Add to .gitignore:
venv/
.venv/
env/
Installing Packages
# Activate first
source venv/bin/activate
# Install packages
pip install requests
pip install flask pandas numpy
# Check what's installed
pip listrequirements.txt
Save dependencies:
pip freeze > requirements.txtInstall from file:
pip install -r requirements.txtExample requirements.txt:
requests==2.31.0
flask==3.0.0
pandas==2.1.0
Better: Pin Versions
Unpinned dependencies cause problems:
# Bad - versions can change
requests
flask
# Good - exact versions
requests==2.31.0
flask==3.0.0
Even better, use ranges:
requests>=2.31.0,<3.0.0
flask>=3.0.0,<4.0.0
Dev Dependencies
Separate production and development:
# requirements.txt (production)
requests==2.31.0
flask==3.0.0
# requirements-dev.txt
-r requirements.txt
pytest==7.4.0
black==23.7.0
mypy==1.5.0
Install dev dependencies:
pip install -r requirements-dev.txtUsing pyproject.toml
Modern approach:
[project]
name = "myproject"
version = "0.1.0"
dependencies = [
"requests>=2.31.0",
"flask>=3.0.0",
]
[project.optional-dependencies]
dev = [
"pytest>=7.4.0",
"black>=23.7.0",
]Install:
pip install -e . # Production
pip install -e ".[dev]" # With dev dependenciespip-tools for Better Management
pip install pip-toolsCreate requirements.in:
requests
flask
Compile with pinned versions:
pip-compile requirements.inGenerates requirements.txt with exact versions and hashes.
Update dependencies:
pip-compile --upgrade requirements.inCommon Issues
Wrong Python Version
# Check which Python
which python
python --version
# Create with specific version
python3.11 -m venv venvPackages Not Found
Did you activate the environment?
source venv/bin/activate
which pip # Should show venv pathEnvironment Corruption
Delete and recreate:
rm -rf venv
python -m venv venv
source venv/bin/activate
pip install -r requirements.txtAlternatives
virtualenv
More features than venv:
pip install virtualenv
virtualenv venvconda
For data science with non-Python dependencies:
conda create -n myenv python=3.11
conda activate myenv
conda install numpy pandasPoetry
All-in-one dependency management:
poetry new myproject
poetry add requests
poetry installPDM
PEP-compliant modern tool:
pdm init
pdm add requests
pdm installMy Workflow
# New project
mkdir myproject && cd myproject
python -m venv venv
source venv/bin/activate
# Install packages
pip install requests flask
# Save dependencies
pip freeze > requirements.txt
# Later / on another machine
python -m venv venv
source venv/bin/activate
pip install -r requirements.txtSimple, reliable, works everywhere.
Quick Reference
| Command | Purpose |
|---|---|
python -m venv venv | Create environment |
source venv/bin/activate | Activate (Unix) |
venv\Scripts\activate | Activate (Windows) |
deactivate | Deactivate |
pip install X | Install package |
pip freeze > requirements.txt | Save dependencies |
pip install -r requirements.txt | Install from file |
Virtual environments are essential. Use them for every project.