Skip to main content
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 bare python, 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.toml fully configured
  • src/ 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:
Run with:
  • 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

Don’t use uv pip installUse uv add and uv sync instead. uv pip is a compatibility shim.
Don’t manually edit pyproject.toml to add dependenciesUse uv add <pkg> and uv remove <pkg>. This keeps pyproject.toml and uv.lock in sync.
Don’t manually activate virtual environmentsUse uv run <cmd> for all commands. uv manages the virtualenv automatically.
Don’t use [project.optional-dependencies] for dev toolsUse [dependency-groups] (PEP 735) instead. This is the modern standard.

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
  • 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