snippetpythonModeratepending
Python virtual environments and dependency isolation
Viewed 0 times
venvvirtual environmentpippip-toolsuvdependencies
Problem
Need to isolate project dependencies and avoid conflicts between different Python projects.
Solution
Modern Python dependency management:
Project structure:
Add to
# Built-in venv (recommended for most cases)
python3 -m venv .venv
source .venv/bin/activate # Linux/macOS
# .venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Freeze current state
pip freeze > requirements.txt
# Better: use pip-tools for reproducible builds
pip install pip-tools
# requirements.in (what you want)
django>=4.2
celery
redis
# Compile to locked versions
pip-compile requirements.in # -> requirements.txt with exact versions
pip-sync requirements.txt # Install exactly what's specified# Modern alternative: uv (much faster)
pip install uv
uv venv
uv pip install -r requirements.txt
# Or use uv directly without activating:
uv run python script.pyProject structure:
project/
.venv/ # Never commit this
requirements.in # Direct dependencies
requirements.txt # Locked (compiled) dependencies
requirements-dev.txt # Dev-only dependencies
pyproject.toml # Modern project metadataAdd to
.gitignore: .venv/, __pycache__/, *.pycWhy
Without virtual environments, all projects share the global Python, causing version conflicts between projects.
Context
Python project setup and dependency management
Revisions (0)
No revisions yet.