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

More Python tasks

Back to the full Python cheat sheet