new-python-project
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython 项目初始化
Python Project Initialization
流程概述
Process Overview
- 确认项目信息 - 与用户交互确认项目名称和 Python 版本
- 初始化项目 - 使用 uv init 创建项目,启用 git 版本控制
- 配置代码规范检查 - 使用 pre-commit 管理并初始化代码规范检查 hooks,并应用相应的规范配置
- 配置测试框架 - 添加 pytest、pytest-cov 开发依赖
- 配置版本管理工具 - 配置 .gitignore 优化VCS管理,配置版本元信息,配置 bumpversion 进行语义化版本管理
- 添加常用开发工具 - 添加 ipython 用于本地调试
- 安装并验证 - 安装所有依赖并验证配置
- 更新README - 创建并更新README文件,添加项目使用说明
- 完成初始提交
- Confirm Project Information - Interact with users to confirm project name and Python version
- Initialize Project - Use uv init to create the project and enable git version control
- Configure Code Style Checking - Use pre-commit to manage and initialize code style checking hooks, and apply corresponding specification configurations
- Configure Testing Framework - Add pytest and pytest-cov as development dependencies
- Configure Version Management Tools - Optimize VCS management by configuring .gitignore, set up version metadata, and configure bumpversion for semantic versioning
- Add Common Development Tools - Add ipython for local debugging
- Install and Verify - Install all dependencies and verify configurations
- Update README - Create and update the README file with project usage instructions
- Complete Initial Commit
Step 1: 确认项目信息
Step 1: Confirm Project Information
与用户确认以下信息:
- 项目名称 project_name: 默认使用当前目录名称
- 项目描述 project_description: 简短的项目描述信息,用于快速理解项目功能和定位
- Python 版本 python_version: 推荐使用官方支持的版本(3.10及以上)
Confirm the following information with users:
- Project Name (project_name): Defaults to the current directory name
- Project Description (project_description): A short project description to quickly understand the project's function and positioning
- Python Version (python_version): Recommended to use officially supported versions (3.10 and above)
Step 2: 初始化项目
Step 2: Initialize Project
使用 uv 初始化项目,启用 git 版本控制:
bash
uv init \
# 项目名称
--name=<project_name> \
# 项目描述
--description="<project_description>" \
# 初始化 src/<project_name> 包目录
--package \
# 初始化 git
--vcs=git \
# Python 最低支持版本
--python=<python_version>Use uv to initialize the project and enable git version control:
bash
uv init \
# Project name
--name=<project_name> \
# Project description
--description="<project_description>" \
# Initialize src/<project_name> package directory
--package \
# Initialize git
--vcs=git \
# Minimum supported Python version
--python=<python_version>Step 3: 配置代码规范检查
Step 3: Configure Code Style Checking
添加 pre-commit 及代码格式化工具:
bash
uv add --dev pre-commit black flake8 flake8-import-order ruff将 assets/pre-commit-config.yaml 内容写入项目根目录的 文件
.pre-commit-config.yaml将 assets/flake8.j2 模板应用到项目根目录的 文件,替换变量 为项目名称
.flake8{{ project_name }}安装 pre-commit hooks:
bash
uv run pre-commit installAdd pre-commit and code formatting tools:
bash
uv add --dev pre-commit black flake8 flake8-import-order ruffWrite the content of assets/pre-commit-config.yaml to the file in the project root directory
.pre-commit-config.yamlApply the template assets/flake8.j2 to the file in the project root directory, replacing the variable with the project name
.flake8{{ project_name }}Install pre-commit hooks:
bash
uv run pre-commit installStep 4: 配置测试框架
Step 4: Configure Testing Framework
添加 pytest 和 pytest-cov 开发依赖:
bash
uv add --dev pytest pytest-cov创建 tests 目录和初始测试文件:
bash
mkdir -p tests
touch tests/__init__.py在 中添加 pytest 配置:
pyproject.tomltoml
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_classes = ["Test*", "*Test"]
python_functions = ["test_*"]
addopts = [
"-v",
"--cov=src/<project_name>",
"--cov-report=term-missing",
"--tb=short",
]
[tool.coverage.run]
source = ["src/<project_name>"]
branch = true
omit = ["tests/*"]
[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise NotImplementedError",
"if __name__ == .__main__.:",
]Add pytest and pytest-cov as development dependencies:
bash
uv add --dev pytest pytest-covCreate tests directory and initial test files:
bash
mkdir -p tests
touch tests/__init__.pyAdd pytest configuration to :
pyproject.tomltoml
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_classes = ["Test*", "*Test"]
python_functions = ["test_*"]
addopts = [
"-v",
"--cov=src/<project_name>",
"--cov-report=term-missing",
"--tb=short",
]
[tool.coverage.run]
source = ["src/<project_name>"]
branch = true
omit = ["tests/*"]
[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise NotImplementedError",
"if __name__ == .__main__.:",
]Step 5: 配置版本管理工具
Step 5: Configure Version Management Tools
添加 .gitignore
bash
curl https://raw.githubusercontent.com/github/gitignore/refs/heads/main/Python.gitignore -o .gitignore添加 bumpversion 依赖:
bash
uv add --dev bumpversion配置版本元信息
-
新建文件用于保存项目版本元信息
src/<project_name>/_version.pypython__version__ = "0.1.0" -
将版本信息导出到项目包命名空间,调整
src/<project_name>/__init__.pypython"""<project_name> package.""" from ._version import __version__ # noqa: F401 -
添加测试用例用于验证版本元信息获取python
from <project_name> import __version__ def test_version(): assert __version__
将 assets/bumpversion.cfg.j2 模板应用到项目根目录的 文件,替换变量 为项目名称
.bumpversion.cfg{{ project_name }}Add .gitignore
bash
curl https://raw.githubusercontent.com/github/gitignore/refs/heads/main/Python.gitignore -o .gitignoreAdd bumpversion dependency:
bash
uv add --dev bumpversionConfigure Version Metadata
-
Create afile to store project version metadata
src/<project_name>/_version.pypython__version__ = "0.1.0" -
Export version information to the project package namespace, adjust
src/<project_name>/__init__.pypython"""<project_name> package.""" from ._version import __version__ # noqa: F401 -
Add test case to verify version metadata retrievalpython
from <project_name> import __version__ def test_version(): assert __version__
Apply the template assets/bumpversion.cfg.j2 to the file in the project root directory, replacing the variable with the project name
.bumpversion.cfg{{ project_name }}Step 6: 添加常用开发工具
Step 6: Add Common Development Tools
bash
uv add --dev ipythonbash
uv add --dev ipythonStep 7: 安装并验证
Step 7: Install and Verify
安装所有依赖:
bash
uv sync验证配置:
bash
undefinedInstall all dependencies:
bash
uv syncVerify configurations:
bash
undefined验证 pytest
Verify pytest
uv run pytest
uv run pytest
验证 pre-commit
Verify pre-commit
uv run pre-commit run --all-files
uv run pre-commit run --all-files
验证 bumpversion
Verify bumpversion
uv run bumpversion --help
uv run bumpversion --help
验证版本元信息
Verify version metadata
uv run python -c 'from <project_name> import version; print(version)'
undefineduv run python -c 'from <project_name> import version; print(version)'
undefinedStep 8: 更新README
Step 8: Update README
使用模板 assets/README.md.j2 生成项目 README 文件,替换以下变量:
- - 项目名称
{{ project_name }} - - 项目描述
{{ project_description }}
将生成的内容写入项目根目录的 文件。
README.mdGenerate the project README file using the template assets/README.md.j2, replacing the following variables:
- - Project name
{{ project_name }} - - Project description
{{ project_description }}
Write the generated content to the file in the project root directory.
README.mdStep 9: 完成初始提交
Step 9: Complete Initial Commit
bash
git add .
git commit -m "Initial project setup"bash
git add .
git commit -m "Initial project setup"