Virtual Environments & Packaging in Python
From the Python cheat sheet ยท Modules & Virtual Environments ยท verified Jul 2026
Virtual Environments & Packaging
Isolate project dependencies with venv, pip, or the modern uv toolchain.
python
# === Modern: uv (Astral) โ fastest, all-in-one ===
# Install uv: https://docs.astral.sh/uv/
uv init my-project # Create new project (pyproject.toml + venv)
cd my-project
uv add requests flask # Install + lock deps
uv run python app.py # Run inside the project env
uv sync # Install from uv.lock
# === Classic: venv + pip ===
python3 -m venv venv
# Activate
source venv/bin/activate # macOS / Linux
venv\Scripts\activate # Windows
pip install requests flask
pip freeze > requirements.txt
pip install -r requirements.txt
deactivate๐ก uv replaces pip + venv + pip-tools + pyenv โ much faster, single tool
โก Always use a venv per project โ never install globally
๐ pyproject.toml is the modern config; requirements.txt still works fine
๐ข Commit uv.lock (or pip-tools requirements.txt) so installs are reproducible