Best practices and common patterns in uv
mainTo ensure compatibility and leverage uv's speed and reliability, follow these patterns:
Use uv add instead of pip install in uv projects
In projects managed by uv, do not use pip to add packages, as it bypasses the lockfile.
Use uv run instead of calling python directly
Always prefix your commands with uv run to ensure they execute within the correct managed environment.
Avoid manual environment management
Do not manually create or activate virtual environments using python -m venv or source .venv/bin/activate. Let uv run handle the environment automatically.
# Good: Add dependencies via uv
uv add requests
# Good: Run scripts via uv
uv run script.py
# Good: Run Python commands via uv
uv run python -c "import os; print(os.getcwd())"
# Good: Run specific Python versions via uvx
uvx python@3.12 -c "import sys; print(sys.version)"