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 3.12(最低支持版本)
- Python 3.13
- Python 3.14(最新!)
每个版本都有独立的环境,确保完全兼容性测试。
- 4 层架构:Pre-commit → 多版本测试 → 跨平台 → 完整检查
- 智能缓存:Pixi 环境 + Pre-commit hooks
- 并发控制:自动取消旧的运行
- 路径过滤:只在相关文件变化时触发
- 速度提升:相比传统 CI
- MkDocs + Material - 现代化文档主题
- mkdocstrings - 自动 API 文档生成
- GitHub Pages - 自动部署
curl -fsSL https://pixi.sh/install.sh | bash# 复制模板
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"编辑以下文件以适配你的项目:
[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"[workspace]
name = "my-awesome-project" # 修改项目名
version = "0.1.0"
description = "Your project description" # 修改描述如果需要文档,创建 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# 安装所有环境
pixi install --all
# 安装 pre-commit hooks
pixi run hooks-install# 进入开发环境(Python 3.14)
pixi shell
# 或使用特定 Python 版本
pixi shell -e py312 # Python 3.12
pixi shell -e py313 # Python 3.13pixi 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 # 完整兼容性检查pixi run hooks-install # 安装 hooks
pixi run hooks-run # 手动运行所有 hookspixi 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 # 项目文档
| 环境 | 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 | 生产环境 |
所有 hooks 都通过 Pixi 运行,确保本地和 CI 环境完全一致:
- ✅ Trailing whitespace
- ✅ End of files
- ✅ YAML check
- ✅ Large files check
- ✅ JSON check
- ✅ TOML check
- ✅ Merge conflicts
- ✅ Debug statements
- ✅ Mixed line endings
- ✅ Private key detection
- ✅ Ruff lint
- ✅ Ruff format
- ✅ MyPy type check
- ✅ Bandit security
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。首次使用需要配置:
- 进入仓库 Settings → Pages
- Build and deployment → Source 选择 GitHub Actions
- 推送到
main分支时,文档会自动构建和部署 - 文档地址:
https://<username>.github.io/<repo-name>/
# 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# 本地开发 - 只测当前环境
pixi run test
# 推送前 - 测试所有版本
pixi run test-all-versions
# CI 会自动运行 - 跨平台 + 完整测试# 本地预览
pixi run -e docs docs-serve
# 推送后自动部署
git add docs/
git commit -m "docs: update documentation"
git push# 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
基于以下优秀项目:
- Pixi - 现代包管理
- Ruff - 极速 Linter
- MyPy - 静态类型检查
- MkDocs Material - 漂亮的文档主题
💡 提示:这个模板代表了 2025 年 Python 项目的最佳实践。克隆并开始你的下一个伟大项目吧!