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
Security Tools
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: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
1
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
2
Manual Setup (Alternative)
Create project structure:This creates:
3
Configure pyproject.toml
Key sections:
4
Install Dependencies
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
1
Determine Script vs Project
For standalone scripts: Convert to PEP 723 inline metadataFor projects: Continue to next step
2
Initialize uv
3
Add Dependencies
4
Cleanup
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