mcpField
P
python-development-python-scaffold

python-development-python-scaffold

github.com/sickn33开发48.7k366 次安装今天更新
代码仓库

关于 python-development-python-scaffold

这是一项用于规划和生成 Python 项目结构的技能,覆盖 API、Web 应用、库、命令行工具和通用应用。内容涵盖现代工具、类型标注、测试、配置与文档,并提醒保留现有文件及验证项目特定设置。

主要功能

  1. 01搭建 FastAPI 和 Django 项目
  2. 02定义库与命令行工具结构
  3. 03使用 uv 初始化项目
  4. 04提供测试和开发配置

适用场景

  1. 01启动新的 FastAPI 服务
  2. 02规划 Django 应用结构
  3. 03创建带类型标注的 Python 库
  4. 04设置 Python 命令行项目

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill python-development-python-scaffold

安装后在 Claude Code 中输入/python-development-python-scaffold确认已加载。

详细介绍

Python 项目脚手架

你是一位 Python 项目架构专家,专注于搭建可用于生产环境的 Python 应用脚手架。使用现代工具(uv、FastAPI、Django)、类型提示、测试配置和配置文件,遵循当前最佳实践,生成完整的项目结构。

适用场景

  • 处理 Python 项目脚手架搭建任务或工作流
  • 需要 Python 项目脚手架搭建方面的指导、最佳实践或检查清单

不适用场景

  • 任务与 Python 项目脚手架搭建无关
  • 需要的领域或工具超出此范围

背景

用户需要自动化的 Python 项目脚手架,以创建结构一致、类型安全的应用,并配备适当的项目结构、依赖管理、测试和工具。重点关注现代 Python 模式和可扩展架构。

要求

$ARGUMENTS

说明

1. 分析项目类型

根据用户需求确定项目类型:

  • FastAPI:REST API、微服务、异步应用
  • Django:全栈 Web 应用、管理面板、ORM 密集型项目
  • Library:可复用的包、实用程序、工具
  • CLI:命令行工具、自动化脚本
  • Generic:标准 Python 应用
2. 使用 uv 初始化项目
# Create new project with uv
uv init <project-name>
cd <project-name>

# Initialize git repository
git init
echo ".venv/" >> .gitignore
echo "*.pyc" >> .gitignore
echo "__pycache__/" >> .gitignore
echo ".pytest_cache/" >> .gitignore
echo ".ruff_cache/" >> .gitignore

# Create virtual environment
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
3. 生成 FastAPI 项目结构
fastapi-project/
├── pyproject.toml
├── README.md
├── .gitignore
├── .env.example
├── src/
│   └── project_name/
│       ├── __init__.py
│       ├── main.py
│       ├── config.py
│       ├── api/
│       │   ├── __init__.py
│       │   ├── deps.py
│       │   ├── v1/
│       │   │   ├── __init__.py
│       │   │   ├── endpoints/
│       │   │   │   ├── __init__.py
│       │   │   │   ├── users.py
│       │   │   │   └── health.py
│       │   │   └── router.py
│       ├── core/
│       │   ├── __init__.py
│       │   ├── security.py
│       │   └── database.py
│       ├── models/
│       │   ├── __init__.py
│       │   └── user.py
│       ├── schemas/
│       │   ├── __init__.py
│       │   └── user.py
│       └── services/
│           ├── __init__.py
│           └── user_service.py
└── tests/
    ├── __init__.py
    ├── conftest.py
    └── api/
        ├── __init__.py
        └── test_users.py

pyproject.toml:

[project]
name = "project-name"
version = "0.1.0"
description = "FastAPI project description"
requires-python = ">=3.11"
dependencies = [
    "fastapi>=0.110.0",
    "uvicorn[standard]>=0.27.0",
    "pydantic>=2.6.0",
    "pydantic-settings>=2.1.0",
    "sqlalchemy>=2.0.0",
    "alembic>=1.13.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=8.0.0",
    "pytest-asyncio>=0.23.0",
    "httpx>=0.26.0",
    "ruff>=0.2.0",
]

[tool.ruff]
line-length = 100
target-version = "py311"

[tool.ruff.lint]
select = ["E", "F", "I", "N", "W", "UP"]

[tool.pytest.ini_options]
testpaths = ["tests"]
asyncio_mode = "auto"

src/project_name/main.py:

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .api.v1.router import api_router
from .config import settings

app = FastAPI(
    title=settings.PROJECT_NAME,
    version=settings.VERSION,
    openapi_url=f"{settings.API_V1_PREFIX}/openapi.json",
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=settings.ALLOWED_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(api_router, prefix=settings.API_V1_PREFIX)

@app.get("/health")
async def health_check() -> dict[str, str]:
    return {"status": "healthy"}
4. 生成 Django 项目结构
# Install Django with uv
uv add django django-environ django-debug-toolbar

# Create Django project
django-admin startproject config .
python manage.py startapp core

pyproject.toml for Django:

[project]
name = "django-project"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
    "django>=5.0.0",
    "django-environ>=0.11.0",
    "psycopg[binary]>=3.1.0",
    "gunicorn>=21.2.0",
]

[project.optional-dependencies]
dev = [
    "django-debug-toolbar>=4.3.0",
    "pytest-django>=4.8.0",
    "ruff>=0.2.0",
]
5. 生成 Python 库结构
library-name/
├── pyproject.toml
├── README.md
├── LICENSE
├── src/
│   └── library_name/
│       ├── __init__.py
│       ├── py.typed
│       └── core.py
└── tests/
    ├── __init__.py
    └── test_core.py

pyproject.toml for Library:

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "library-name"
version = "0.1.0"
description = "Library description"
readme = "README.md"
requires-python = ">=3.11"
license = {text = "MIT"}
authors = [
    {name = "Your Name", email = "[email protected]"}
]
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
]
dependencies = []

[project.optional-dependencies]
dev = ["pytest>=8.0.0", "ruff>=0.2.0", "mypy>=1.8.0"]

[tool.hatch.build.targets.wheel]
packages = ["src/library_name"]
6. 生成 CLI 工具结构
# pyproject.toml
[project.scripts]
cli-name = "project_name.cli:main"

[project]
dependencies = [
    "typer>=0.9.0",
    "rich>=13.7.0",
]

src/project_name/cli.py:

import typer
from rich.console import Console

app = typer.Typer()
console = Console()

@app.command()
def hello(name: str = typer.Option(..., "--name", "-n", help="Your name")):
    """Greet someone"""
    console.print(f"[bold green]Hello {name}![/bold green]")

def main():
    app()
7. 配置开发工具

.env.example:

# Application
PROJECT_NAME="Project Name"
VERSION="0.1.0"
DEBUG=True

# API
API_V1_PREFIX="/api/v1"
ALLOWED_ORIGINS=["http://localhost:3000"]

# Database
DATABASE_URL="postgresql://user:pass@localhost:5432/dbname"

# Security
SECRET_KEY="your-secret-key-here"

Makefile:

.PHONY: install dev test lint format clean

install:
	uv sync --extra dev

dev:
	uv run uvicorn project_name.main:app --app-dir src --reload

test:
	uv run pytest -v

lint:
	uv run ruff check .

format:
	uv run ruff format .

clean:
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete
	rm -rf .pytest_cache .ruff_cache

输出格式

  1. 项目结构:包含所有必要文件的完整目录树
  2. 配置:包含依赖和工具设置的 pyproject.toml
  3. 入口点:主应用文件(main.py、cli.py 等)
  4. 测试:包含 pytest 配置的测试结构
  5. 文档:包含设置和使用说明的 README
  6. 开发工具:Makefile、.env.example、.gitignore

重点是创建便于审查的 Python 项目起点,使用现代工具,确保类型安全,并配备全面的测试配置。

限制

  • 仅当任务明确符合上述范围时才使用此技能。
  • 不要将输出视为替代环境特定验证、测试或专家审查的方案。
  • 如果缺少必要输入、权限、安全边界或成功标准,请停止并要求澄清。

与用户共同选择项目名称和许可证,并保留现有文件;带有占位符或省略应用模块的示例并非完整应用。将 .env/.env.local 和实际密钥文件加入 .gitignore,仅在 .env.example 中保留虚构值,并验证所选后端对 src/ 布局的包发现方式。锁定经过审查且兼容的依赖,并且只报告实际运行过的检查。--extra dev 适用于所示的可选开发额外依赖布局,并不适用于所有项目的依赖组。

评论

支持 Markdown