mcpField
P
python-development-python-scaffold

python-development-python-scaffold

github.com/rmyndharis开发1.9k46 次安装2 周前更新
代码仓库

关于 python-development-python-scaffold

用于 Python 项目脚手架设计的技能,根据项目类型提供目录结构、配置和开发工具建议,覆盖 FastAPI、Django、库、命令行工具及通用应用。

主要功能

  1. 01按项目类型规划架构
  2. 02使用 uv 初始化项目
  3. 03生成 FastAPI 与 Django 结构
  4. 04搭建库和命令行工具结构
  5. 05配置测试与开发工具

适用场景

  1. 01创建 FastAPI 服务项目
  2. 02搭建 Django 全栈应用
  3. 03初始化可复用 Python 库
  4. 04构建 Python 命令行工具

安装

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

$npx skills add https://github.com/rmyndharis/antigravity-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

Django 的 pyproject.toml:

[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:

[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

dev:
	uv run uvicorn src.project_name.main:app --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 项目,并配备现代工具、类型安全和完善的测试配置。

评论

支持 Markdown