python-conventions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython Conventions
Python 开发约定
Apply these conventions when working on Python files or projects.
在处理 Python 文件或项目时请遵循以下约定。
Dispatch
触发规则
| $ARGUMENTS | Action |
|---|---|
| Active (auto-invoked when working on Python files) | Apply all conventions below |
| Empty | Display convention summary |
| Verify tooling compliance only |
| $ARGUMENTS | 操作 |
|---|---|
| 生效(处理 Python 文件时自动触发) | 应用以下所有约定 |
| 无参数 | 展示约定摘要 |
| 仅校验工具使用是否符合规范 |
References
参考文档
| File | Purpose |
|---|---|
| When to break conventions (legacy, corporate) |
| 文件 | 用途 |
|---|---|
| 可突破约定的场景(遗留项目、企业特定要求) |
Tooling
工具使用规范
- Package manager: for all Python operations (
uvinstead ofuv run python ...,pythoninstead ofuv add)pip install - Project config: with
pyproject.toml(neveruv add <pkg>oruv pip install)pip install - Type checking: (not mypy)
ty - Linting: and
ruff checkruff format - Testing:
uv run pytest - Task running: for all script execution
uv run <command>
- 包管理器:所有 Python 操作都使用 (使用
uv替代uv run python ...,使用python替代uv add)pip install - 项目配置:通过 管理
uv add <pkg>(禁止使用pyproject.toml或uv pip install)pip install - 类型检查:使用 (而非 mypy)
ty - 代码检查与格式化:使用 和
ruff checkruff format - 测试:使用
uv run pytest - 任务运行:所有脚本执行都添加 前缀
uv run <command>
Virtual Environments
虚拟环境
- Let manage virtual environments automatically
uv - Never manually create or activate directories
.venv - Use to execute within the project environment
uv run
- 交由 自动管理虚拟环境
uv - 禁止手动创建或激活 目录
.venv - 使用 在项目环境中执行命令
uv run
Preferred Libraries
推荐依赖库
When applicable, prefer these libraries over alternatives:
| Purpose | Library | Instead of |
|---|---|---|
| Logging | | |
| Retries | | manual retry loops |
| Progress bars | | print statements |
| Web APIs | | flask |
| CLI tools | | argparse, click |
| DB migrations | | manual SQL |
| DB ORM | | sqlalchemy raw |
| UI/demos | | streamlit |
| Numerics | | manual math |
| MCP servers | | raw MCP protocol |
如果适用,优先选择以下库而非替代方案:
| 用途 | 依赖库 | 替代方案 |
|---|---|---|
| 日志 | | |
| 重试 | | 手动编写重试循环 |
| 进度条 | | print 语句输出 |
| Web API 开发 | | flask |
| CLI 工具开发 | | argparse、click |
| 数据库迁移 | | 手动编写 SQL |
| 数据库 ORM | | 原生 sqlalchemy |
| UI/演示应用 | | streamlit |
| 数值计算 | | 手动实现数学计算 |
| MCP 服务器开发 | | 原生 MCP 协议实现 |
Project Structure
项目结构
- Use for all project metadata and dependencies
pyproject.toml - Place source code in a package directory matching the project name
- Use workspace members for monorepo sub-packages
uv - Run and
ruff checkbefore committingruff format
- 所有项目元数据和依赖都在 中声明
pyproject.toml - 源代码放在与项目名称同名的包目录下
- Monorepo 子包使用 workspace 成员管理
uv - 提交代码前必须执行 和
ruff checkruff format
Critical Rules
核心规则
- Always use for package management -- never
uvorpip installpip - Use to add dependencies -- never
uv adduv pip install - Use for type checking -- never
tymypy - Run before committing any Python changes
uv run pytest - Run to lint before committing
uv run ruff check - Check before breaking any convention
references/exceptions.md - Prefer libraries from the preferred table over their alternatives
Canonical terms (use these exactly):
- -- the required package manager and task runner
uv - -- the required type checker (not mypy)
ty - -- the required linter and formatter
ruff - -- the single source of project configuration
pyproject.toml - -- prefix for all Python command execution
uv run
- 包管理必须使用 ,禁止使用
uv或直接调用pip installpip - 新增依赖必须使用 ,禁止使用
uv adduv pip install - 类型检查必须使用 ,禁止使用
tymypy - 提交任何 Python 代码变更前必须执行
uv run pytest - 提交前必须执行 做代码检查
uv run ruff check - 若要突破任何约定,请先查阅
references/exceptions.md - 优先选用推荐列表中的库,而非其他替代方案
规范术语(请严格按照以下表述使用):
- -- 要求使用的包管理器和任务运行器
uv - -- 要求使用的类型检查器(而非 mypy)
ty - -- 要求使用的代码检查器和格式化工具
ruff - -- 项目配置的唯一可信源
pyproject.toml - -- 所有 Python 命令执行的前缀
uv run