Modern Python best practices using uv, ruff, ty, and pytest. Based on Trail of Bits cookiecutter-python template.
Overview
The Modern Python skill teaches Claude to use fast, modern Python tooling instead of legacy tools. It includes a SessionStart hook that intercepts barepython, pip, and pipx commands, redirecting to uv equivalents.
Author: William Tan
Core Tools
| Tool | Purpose | Replaces |
|---|---|---|
| uv | Package/dependency management | pip, virtualenv, pip-tools, pipx, pyenv |
| ruff | Linting AND formatting | flake8, black, isort, pyupgrade, pydocstyle |
| ty | Type checking | mypy, pyright |
| pytest | Testing with coverage | unittest |
| prek | Pre-commit hooks | pre-commit (faster, Rust-native) |
Security Tools
| Tool | Purpose | When It Runs |
|---|---|---|
| shellcheck | Shell script linting | pre-commit |
| detect-secrets | Secret detection | pre-commit |
| actionlint | Workflow syntax validation | pre-commit, CI |
| zizmor | Workflow security audit | pre-commit, CI |
| pip-audit | Dependency vulnerability scanning | CI, manual |
| Dependabot | Automated dependency updates | scheduled |
When to Use
- Setting up a new Python project with modern tooling
- Replacing pip/virtualenv with uv for faster dependency management
- Replacing flake8/black/isort with ruff for unified linting/formatting
- Replacing mypy with ty for faster type checking
- Adding pre-commit hooks and security scanning
- Writing Python scripts with external dependencies (PEP 723)
Installation
Legacy Command Interception
This plugin includes a SessionStart hook that intercepts legacy Python commands:| Intercepted Command | Suggested Alternative |
|---|---|
python ... | uv run python ... |
python -m module | uv run python -m module |
python -m pip | uv add/uv remove |
pip install pkg | uv add pkg or uv run --with pkg |
pip uninstall pkg | uv remove pkg |
pip freeze | uv export |
uv pip ... | uv add/uv remove/uv sync |
pipx install <pkg> | uv tool install <pkg> |
pipx run <pkg> | uvx <pkg> |
pipx uninstall <pkg> | uv tool uninstall <pkg> |
pipx upgrade <pkg> | uv tool upgrade <pkg> |
pipx upgrade-all | uv tool upgrade --all |
Commands like
grep python, which python, and cat python.txt work normally because python is a shell argument, not the command being invoked.Quick Start: Minimal Project
For simple multi-file projects not intended for distribution:Full Project Setup
Use Cookiecutter Template (Recommended)
The Trail of Bits cookiecutter template bootstraps a complete project:This creates a project with:
pyproject.tomlfully configuredsrc/layout- Dependency groups for dev/test/docs
- Pre-commit hooks
- GitHub Actions CI
- Security scanning
PEP 723: Standalone Scripts
For single-file scripts with dependencies, use PEP 723 inline metadata:Why PEP 723 instead of requirements.txt?
Why PEP 723 instead of requirements.txt?
- Self-contained - Dependencies declared in the script itself
- Isolated - Each script gets its own environment
- Fast - uv caches dependencies across runs
- Portable - Works on any machine with uv installed
uv Command Reference
Project Management
Running Code
Tool Management
When to Use --with vs uv add
uv add
Package is a project dependency (goes in pyproject.toml/uv.lock)
--with
One-off usage, testing, or scripts outside project context
Migration Guide
From requirements.txt + pip
Determine Script vs Project
For standalone scripts: Convert to PEP 723 inline metadataFor projects: Continue to next step
From flake8 + black + isort
From mypy / pyright
Anti-Patterns to Avoid
Best Practices Checklist
Makefile Template
Reference Documentation
The skill includes detailed reference files:- migration-checklist.md - Step-by-step migration cleanup
- pyproject.md - Complete pyproject.toml reference
- uv-commands.md - uv command reference
- ruff-config.md - Ruff linting/formatting configuration
- testing.md - pytest and coverage setup
- pep723-scripts.md - PEP 723 inline script metadata
- prek.md - Fast pre-commit hooks with prek
- security-setup.md - Security hooks and dependency scanning
- dependabot.md - Automated dependency updates
Related Skills
- Devcontainer Setup - Configures Python 3.13 via uv in devcontainers
- Ask Questions If Underspecified - Used when migration preferences are unclear
- Second Opinion - Can review Python code before committing