| name | python-uv |
| description | Modern Python development with uv package manager. Use when working on Python projects using uv, pytest, FastAPI, or Django. Covers development workflow, testing, and EC2 deployment. |
| allowed-tools | Read, Edit, Write, Bash, Grep, Glob, Task |
Python/uv Development
Modern Python development workflow using uv instead of pip/poetry.
Why uv?
- 10-100x faster than pip
- Drop-in replacement for pip, pip-tools, virtualenv
- Lockfile support (
uv.lock)
- Built-in Python version management
Project Setup
New Project
uv init my-project
cd my-project
uv init my-project --python 3.12
Existing Project
uv sync
uv sync --dev
pyproject.toml Structure
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "my-project"
version = "0.1.0"
description = "Project description"
readme = "README.md"
requires-python = ">=3.12"
authors = [{ name = "Your Name", email = "you@example.com" }]
dependencies = [
"fastapi~=0.118",
"uvicorn[standard]~=0.37",
"pydantic>=2.12,<3",
]
[project.optional-dependencies]
dev = [
"pytest",
"pytest-asyncio",
"pytest-cov",
"ruff",
"black",
]
[project.scripts]
myapp = "app.main:start"
[tool.pytest.ini_options]
asyncio_mode = "auto"
testpaths = ["tests"]
addopts = "-q --tb=short"
[tool.ruff]
target-version = "py312"
line-length = 88
[tool.black]
line-length = 88
target-version = ["py312"]
Development Workflow
Dependency Management
uv add fastapi
uv add --dev pytest
uv add "pydantic>=2.0,<3"
uv remove package-name
uv sync --upgrade
uv add package-name --upgrade
Running Code
uv run python script.py
uv run python -m app.main
uv run myapp
uv run --python 3.12 python script.py
Testing
uv run pytest
uv run pytest --cov=app --cov-report=term-missing
uv run pytest tests/test_api.py
uv run pytest -n auto
uv run pytest -m "not slow"
Linting and Formatting
uv run black .
uv run black --check .
uv run ruff check .
uv run ruff check --fix .
uv run isort .
FastAPI Development
Project Structure
my-project/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI app
│ ├── api/
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── models/
│ │ └── __init__.py
│ └── tests/
│ └── test_api.py
├── pyproject.toml
├── uv.lock
└── README.md
Running FastAPI
uv run uvicorn app.main:app --reload --port 8000
uv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4
Django Development
Running Django
uv run python manage.py migrate
uv run python manage.py runserver
uv run python manage.py createsuperuser
uv run python manage.py shell
Database Migrations (Alembic)
uv run alembic revision --autogenerate -m "Add users table"
uv run alembic upgrade head
uv run alembic downgrade -1
EC2 Deployment
Deploy Script Pattern (from rookery)
SCRIPT_AFTER: |
set -e
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
cd /opt/myapp
uv sync
uv run alembic upgrade head
sudo systemctl restart myapp
for i in {1..5}; do
if curl -sf http://127.0.0.1:8000/api/health; then
echo "Health check passed"
exit 0
fi
sleep 5
done
exit 1
Systemd Service
[Unit]
Description=My FastAPI App
After=network.target
[Service]
User=appuser
WorkingDirectory=/opt/myapp
ExecStart=/home/appuser/.local/bin/uv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 2
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
CI/CD
GitHub Actions
name: Python Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v4
with:
version: "latest"
- name: Set up Python
run: uv python install 3.12
- name: Install dependencies
run: uv sync --dev
- name: Run tests
run: uv run pytest
- name: Lint
run: uv run ruff check .
Common Patterns
Version Stamping
printf "%s\n" "$(date -u +%Y%m%dT%H%M%SZ)_$(git rev-parse --short HEAD)" > VERSION
Environment Variables
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
database_url: str
secret_key: str
debug: bool = False
class Config:
env_file = ".env"
settings = Settings()
Quick Reference
uv sync
uv run pytest
uv run ruff check .
uv run black .
uv add <pkg>
uv add --dev <pkg>
uv run uvicorn app.main:app --reload
uv run python manage.py runserver
uv run alembic upgrade head