Skip to content

cagedbird043/python-project-template

Repository files navigation

Python Project Template 🚀

CI Documentation codecov Python 3.12-3.14 Code style: ruff License: MIT

A modern Python project template with best practices built-in.

这是一个包含所有最佳实践的 Python 项目模板,基于 Pixi + 现代化工具链。

📖 查看完整文档

✨ 特性

🔧 开发工具

  • Pixi - 快速、可靠的包管理和环境管理
  • Ruff - 极速 Linter + Formatter(替代 Black、isort、Flake8)
  • MyPy - 严格的类型检查
  • Bandit - 安全漏洞扫描
  • pytest - 现代化测试框架
  • pre-commit - Git hooks 自动化

🐍 Python 版本支持

  • Python 3.12(最低支持版本)
  • Python 3.13
  • Python 3.14(最新!)

每个版本都有独立的环境,确保完全兼容性测试。

🔄 CI/CD 优化

  • 4 层架构:Pre-commit → 多版本测试 → 跨平台 → 完整检查
  • 智能缓存:Pixi 环境 + Pre-commit hooks
  • 并发控制:自动取消旧的运行
  • 路径过滤:只在相关文件变化时触发
  • 速度提升:相比传统 CI

📚 文档自动化

  • MkDocs + Material - 现代化文档主题
  • mkdocstrings - 自动 API 文档生成
  • GitHub Pages - 自动部署

🚀 快速开始

1. 安装 Pixi

curl -fsSL https://pixi.sh/install.sh | bash

2. 创建你的项目

# 复制模板
cp -r python-project-template my-awesome-project
cd my-awesome-project

# 初始化 Git
git init
git add .
git commit -m "feat: initialize project from template"

3. 自定义配置

编辑以下文件以适配你的项目:

pyproject.toml

[project]
name = "my-awesome-project"  # 修改项目名
version = "0.1.0"
description = "Your project description"  # 修改描述
authors = [{ name = "Your Name", email = "you@example.com" }]  # 修改作者

[project.urls]
Homepage = "https://github.com/yourusername/your-project"  # 修改链接
Repository = "https://github.com/yourusername/your-project.git"
Issues = "https://github.com/yourusername/your-project/issues"

pixi.toml

[workspace]
name = "my-awesome-project"  # 修改项目名
version = "0.1.0"
description = "Your project description"  # 修改描述

mkdocs.yml

如果需要文档,创建 mkdocs.yml

site_name: My Awesome Project
site_description: Your project description
site_author: Your Name
repo_url: https://github.com/yourusername/your-project
repo_name: yourusername/your-project

theme:
  name: material
  palette:
    - scheme: default
      primary: indigo
      accent: indigo
      toggle:
        icon: material/brightness-7
        name: Switch to dark mode
    - scheme: slate
      primary: indigo
      accent: indigo
      toggle:
        icon: material/brightness-4
        name: Switch to light mode

nav:
  - Home: index.md
  - Getting Started:
      - Installation: getting-started/installation.md
      - Quick Start: getting-started/quickstart.md
  - API Reference:
      - Module: api/module.md

4. 安装依赖

# 安装所有环境
pixi install --all

# 安装 pre-commit hooks
pixi run hooks-install

5. 开始开发

# 进入开发环境(Python 3.14)
pixi shell

# 或使用特定 Python 版本
pixi shell -e py312  # Python 3.12
pixi shell -e py313  # Python 3.13

📋 可用命令

代码质量检查

pixi run format          # 格式化代码
pixi run lint            # Lint 检查
pixi run typecheck       # 类型检查
pixi run security        # 安全检查
pixi run test            # 运行测试

多版本测试

pixi run show-versions        # 显示所有 Python 版本
pixi run test-all-versions    # 在所有版本上测试
pixi run check-compat         # 完整兼容性检查

Pre-commit

pixi run hooks-install   # 安装 hooks
pixi run hooks-run       # 手动运行所有 hooks

文档

pixi run -e docs docs-serve    # 本地预览文档
pixi run -e docs docs-build    # 构建文档
# 部署由 GitHub Actions 自动完成,推送到 main 分支即可

构建发布

pixi run build           # 构建包
pixi run clean-build     # 清理构建产物

🏗️ 项目结构

my-awesome-project/
├── .github/
│   └── workflows/
│       ├── ci.yml           # 主 CI 流程
│       ├── docs.yml         # 文档构建和部署
│       ├── pr-check.yml     # PR 快速检查
│       └── release.yml      # 发布流程
├── src/
│   └── __init__.py          # 你的源代码
├── tests/
│   └── __init__.py          # 测试代码
├── docs/                    # 文档文件
├── examples/                # 示例代码
├── pixi.toml                # Pixi 配置
├── pyproject.toml           # Python 项目配置
├── .pre-commit-config.yaml  # Pre-commit hooks
├── .gitignore               # Git 忽略规则
└── README.md                # 项目文档

🔧 配置说明

Pixi 环境

环境 Python 版本 用途
default 3.14 默认开发环境
py312 3.12 Python 3.12 测试
py313 3.13 Python 3.13 测试
py314 3.14 Python 3.14 测试
docs 3.14 文档构建
prod 3.14 生产环境

Pre-commit Hooks

所有 hooks 都通过 Pixi 运行,确保本地和 CI 环境完全一致:

  1. ✅ Trailing whitespace
  2. ✅ End of files
  3. ✅ YAML check
  4. ✅ Large files check
  5. ✅ JSON check
  6. ✅ TOML check
  7. ✅ Merge conflicts
  8. ✅ Debug statements
  9. ✅ Mixed line endings
  10. ✅ Private key detection
  11. ✅ Ruff lint
  12. ✅ Ruff format
  13. ✅ MyPy type check
  14. ✅ Bandit security

CI/CD 流程

Layer 1: Pre-commit Checks (< 5 min)

  • 运行所有 pre-commit hooks
  • 快速反馈代码质量问题

Layer 2: 多版本兼容性测试 (< 8 min)

  • 在 Python 3.12、3.13、3.14 上测试
  • 并行运行,fail-fast 策略

Layer 3: 跨平台测试 (< 10 min)

  • macOS 和 Windows 测试
  • 只测试最新 Python 版本

Layer 4: 完整质量检查 (< 15 min)

  • 仅在 main 分支或 tag 上运行
  • 完整兼容性检查
  • 构建包

GitHub Pages 部署

文档会自动部署到 GitHub Pages。首次使用需要配置

  1. 进入仓库 SettingsPages
  2. Build and deploymentSource 选择 GitHub Actions
  3. 推送到 main 分支时,文档会自动构建和部署
  4. 文档地址: https://<username>.github.io/<repo-name>/

🎯 最佳实践

1. 开发流程

# 1. 创建功能分支
git checkout -b feature/awesome-feature

# 2. 开发代码
# ... 编写代码 ...

# 3. 运行检查
pixi run check  # 快速检查
pixi run check-all  # 完整检查

# 4. 提交(pre-commit 会自动运行)
git add .
git commit -m "feat: add awesome feature"

# 5. 推送
git push origin feature/awesome-feature

2. 测试策略

# 本地开发 - 只测当前环境
pixi run test

# 推送前 - 测试所有版本
pixi run test-all-versions

# CI 会自动运行 - 跨平台 + 完整测试

3. 文档更新

# 本地预览
pixi run -e docs docs-serve

# 推送后自动部署
git add docs/
git commit -m "docs: update documentation"
git push

4. 发布流程

# 1. 更新版本号
# 编辑 pyproject.toml 和 pixi.toml

# 2. 创建 tag
git tag -a v1.0.0 -m "Release v1.0.0"
git push origin v1.0.0

# 3. CI 会自动:
#    - 运行所有检查
#    - 构建包
#    - 创建 GitHub Release
#    - 发布到 PyPI

📊 性能优势

相比传统 setup.py + requirements.txt 项目:

指标 传统方式 此模板 提升
CI 总时间 25-30 min 8-10 min 66-87%
环境安装 5-8 min 1-2 min 75%
缓存命中率 40-60% 85-95% 40%
Pre-commit 速度 30-45s 5-10s 80%

🤝 贡献

这个模板本身也欢迎改进!如果你有更好的实践,请提交 PR。

📄 许可证

MIT License

🙏 致谢

基于以下优秀项目:


💡 提示:这个模板代表了 2025 年 Python 项目的最佳实践。克隆并开始你的下一个伟大项目吧!

Releases

No releases published

Packages

No packages published

Languages