关于 Cookiecutter
Cookiecutter 是一个命令行工具,使用交互式变量从预定义模板生成项目。开发者和编码 Agent 可以自动化项目初始化,避免重复的手动设置步骤。
主要功能
- 01从本地或远程 Git 仓库加载项目模板
- 02通过交互式提示收集用户输入并填充模板变量
- 03生成完整的项目目录结构、配置文件和样板代码
- 04支持模板中的条件逻辑和动态文件生成
适用场景
- 01快速引导新的 Python 包、Web 应用或微服务项目
- 02为团队创建和维护标准化的项目模板
- 03集成到 AI Agent 的工作流中自动生成项目骨架
安装
选择你的系统或包管理器,复制命令到终端运行。
$
安装完成后运行cookiecutter --version确认安装成功。
命令示例
常用命令按使用顺序排列。
01
详细介绍
Cookiecutter
使用此命令行工具,通过 cookiecutter(项目模板)快速创建项目。它非常适合生成 Python 包项目及其他项目。
安装
使用 uv 将 Cookiecutter 安装为 CLI 工具:
uv tool install cookiecutter
功能
- **跨平台:**支持 Windows、Mac 和 Linux。
- **易于使用:**无需 Python 知识。
- **用途广泛:**兼容 Python 3.10 至 3.14。
- **支持多种语言:**可使用任何语言或标记格式的模板。
面向用户
快速入门
使用 Cookiecutter 最常见的方式是将其作为命令行工具,配合托管在 GitHub 上的 Cookiecutter 模板使用,例如 https://github.com/audreyfeldroy/cookiecutter-pypackage
使用托管在 GitHub 上的 Cookiecutter 模板
# You'll be prompted to enter values.
# Then it'll create your Python package in the current working directory,
# based on those values.
# For the sake of brevity, repos on GitHub can just use the 'gh' prefix
$ uvx cookiecutter gh:audreyfeldroy/cookiecutter-pypackage
使用本地模板
$ uvx cookiecutter cookiecutter-pypackage/
在 Python 中使用
如果你计划以编程方式使用 Cookiecutter,请运行 uv add cookiecutter 将其添加到项目中。然后即可像这样导入和使用:
from cookiecutter.main import cookiecutter
# Create project from the cookiecutter-pypackage/ template
cookiecutter('cookiecutter-pypackage/')
# Create project from the cookiecutter-pypackage.git repo template
cookiecutter('gh:audreyfeldroy/cookiecutter-pypackage')
如果 Cookiecutter 为你节省了时间,请在 GitHub 上为它点星,让其他开发者也能发现它。
详细用法
- 从本地或远程模板生成项目。
- 使用
cookiecutter.json提示词自定义项目。 - 使用提示前、生成前和生成后钩子。
面向模板创建者
- **支持任何语言和框架。**Cookiecutter 模板只是一个包含变量的目录。它适用于 Python、Rust、Terraform、文档站点,以及任何你需要反复构建的内容。
- **通过钩子完成其余设置。**生成前和生成后的脚本(Shell 或 Python)可处理 git init、依赖安装,以及样板代码所需的其他工作。
- 通过一个文件定义接口。
cookiecutter.json声明所有变量及其默认值。用户回答提示词,模板完成其余工作。
可用模板
在 GitHub 上探索各种开箱即用的模板。
特别推荐的模板
社区
加入社区、参与贡献或寻求帮助。
反馈
我们重视你的反馈。请以建设性的方式分享批评或意见,帮助我们改进。
等待回复?
- 请耐心等待,也可以考虑向社区寻求帮助。
- 如需企业支持,请联系 [email protected]。
行为准则
在项目生态系统内进行所有互动时,请遵守 PyPA 行为准则。
致谢
由 Audrey M. Roy Greenfeld 创建并领导,并由一支尽职的维护者和贡献者团队提供支持。
评论
支持 Markdown