Loading...
Loading...
Compare original and translation side by side
pyproject.tomlpyproject.toml| Avoid | Use Instead |
|---|---|
| |
| |
| Editing pyproject.toml manually to add deps | |
| |
| Poetry | uv (faster, simpler, better ecosystem integration) |
| requirements.txt | PEP 723 for scripts, pyproject.toml for projects |
| mypy / pyright | ty (faster, from Astral team) |
| |
Manual virtualenv activation ( | |
| pre-commit | prek (faster, no Python runtime needed) |
uv adduv removeuv run[dependency-groups][project.optional-dependencies]| 需避免 | 推荐替代方案 |
|---|---|
| |
| |
| 手动编辑pyproject.toml添加依赖 | |
| |
| Poetry | uv(速度更快、更简洁、生态集成更好) |
| requirements.txt | 脚本使用PEP 723,项目使用pyproject.toml |
| mypy / pyright | ty(速度更快,由Astral团队开发) |
使用 | |
手动激活虚拟环境( | |
| pre-commit | prek(速度更快,无需Python运行时) |
uv adduv removeuv run[dependency-groups][project.optional-dependencies]What are you doing?
│
├─ Single-file script with dependencies?
│ └─ Use PEP 723 inline metadata (./references/pep723-scripts.md)
│
├─ New multi-file project (not distributed)?
│ └─ Minimal uv setup (see Quick Start below)
│
├─ New reusable package/library?
│ └─ Full project setup (see Full Setup below)
│
└─ Migrating existing project?
└─ See Migration Guide below你正在做什么?
│
├─ 编写带依赖的单文件脚本?
│ └─ 使用PEP 723内联元数据(./references/pep723-scripts.md)
│
├─ 创建新的多文件项目(无需分发)?
│ └─ 极简uv配置(见下方快速开始)
│
├─ 创建可复用的包/库?
│ └─ 完整项目配置(见下方完整设置)
│
└─ 迁移现有项目?
└─ 见下方迁移指南| Tool | Purpose | Replaces |
|---|---|---|
| uv | Package/dependency management | pip, virtualenv, pip-tools, pipx, pyenv |
| ruff | Linting AND formatting | flake8, black, isort, pyupgrade, pydocstyle |
| ty | Type checking | mypy, pyright (faster alternative) |
| pytest | Testing with coverage | unittest |
| prek | Pre-commit hooks (setup) | pre-commit (faster, Rust-native) |
| 工具 | 用途 | 替代工具 |
|---|---|---|
| uv | 包/依赖管理 | pip、virtualenv、pip-tools、pipx、pyenv |
| ruff | 代码检查+格式化 | flake8、black、isort、pyupgrade、pydocstyle |
| ty | 类型检查 | mypy、pyright(更快的替代方案) |
| pytest | 带覆盖率统计的测试 | unittest |
| prek | 预提交钩子(配置) | pre-commit(速度更快,基于Rust开发) |
| Tool | Purpose | When It Runs |
|---|---|---|
| shellcheck | Shell script linting | pre-commit |
| detect-secrets | Secret detection | pre-commit |
| actionlint | Workflow syntax validation | pre-commit, CI |
| zizmor | Workflow security audit | pre-commit, CI |
| pip-audit | Dependency vulnerability scanning | CI, manual |
| Dependabot | Automated dependency updates | scheduled |
| 工具 | 用途 | 运行时机 |
|---|---|---|
| shellcheck | Shell脚本检查 | 预提交阶段 |
| detect-secrets | 敏感信息检测 | 预提交阶段 |
| actionlint | 工作流语法验证 | 预提交阶段、CI流程 |
| zizmor | 工作流安全审计 | 预提交阶段、CI流程 |
| pip-audit | 依赖漏洞扫描 | CI流程、手动执行 |
| Dependabot | 依赖自动更新 | 定时执行 |
undefinedundefinedundefinedundefineduvx cookiecutter gh:trailofbits/cookiecutter-pythonuvx cookiecutter gh:trailofbits/cookiecutter-pythonuv init --package myproject
cd myprojectmyproject/
├── pyproject.toml
├── README.md
├── src/
│ └── myproject/
│ └── __init__.py
└── .python-versionuv init --package myproject
cd myprojectmyproject/
├── pyproject.toml
├── README.md
├── src/
│ └── myproject/
│ └── __init__.py
└── .python-version[project]
name = "myproject"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = []
[dependency-groups]
dev = [{include-group = "lint"}, {include-group = "test"}, {include-group = "audit"}]
lint = ["ruff", "ty"]
test = ["pytest", "pytest-cov"]
audit = ["pip-audit"]
[tool.ruff]
line-length = 100
target-version = "py311"
[tool.ruff.lint]
select = ["ALL"]
ignore = ["D", "COM812", "ISC001"]
[tool.pytest]
addopts = ["--cov=myproject", "--cov-fail-under=80"]
[tool.ty.terminal]
error-on-warning = true
[tool.ty.environment]
python-version = "3.11"
[tool.ty.rules][project]
name = "myproject"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = []
[dependency-groups]
dev = [{include-group = "lint"}, {include-group = "test"}, {include-group = "audit"}]
lint = ["ruff", "ty"]
test = ["pytest", "pytest-cov"]
audit = ["pip-audit"]
[tool.ruff]
line-length = 100
target-version = "py311"
[tool.ruff.lint]
select = ["ALL"]
ignore = ["D", "COM812", "ISC001"]
[tool.pytest]
addopts = ["--cov=myproject", "--cov-fail-under=80"]
[tool.ty.terminal]
error-on-warning = true
[tool.ty.environment]
python-version = "3.11"
[tool.ty.rules]undefinedundefinedundefinedundefinedundefinedundefined.PHONY: dev lint format test build
dev:
uv sync --all-groups
lint:
uv run ruff format --check && uv run ruff check && uv run ty check src/
format:
uv run ruff format .
test:
uv run pytest
build:
uv build.PHONY: dev lint format test build
dev:
uv sync --all-groups
lint:
uv run ruff format --check && uv run ruff check && uv run ty check src/
format:
uv run ruff format .
test:
uv run pytest
build:
uv buildundefinedundefined
Then:
1. Delete `requirements.txt`, `requirements-dev.txt`
2. Delete virtual environment (`venv/`, `.venv/`)
3. Add `uv.lock` to version control
后续步骤:
1. 删除`requirements.txt`、`requirements-dev.txt`
2. 删除虚拟环境(`venv/`、`.venv/`)
3. 将`uv.lock`加入版本控制uv init --bareuv addinstall_requiresuv add --group dev[project]setup.pysetup.cfgMANIFEST.inuv init --bareuv addinstall_requiresuv add --group dev[project]setup.pysetup.cfgMANIFEST.inuv remove.flake8pyproject.toml [tool.black][tool.isort]uv add --group dev ruffuv run ruff check --fix .uv run ruff format .uv remove.flake8pyproject.toml[tool.black][tool.isort]uv add --group dev ruffuv run ruff check --fix .uv run ruff format .uv removemypy.inipyrightconfig.json[tool.mypy][tool.pyright]uv add --group dev tyuv run ty check src/uv removemypy.inipyrightconfig.jsonpyproject.toml[tool.mypy][tool.pyright]uv add --group dev tyuv run ty check src/| Command | Description |
|---|---|
| Create new project |
| Create distributable package |
| Add dependency |
| Add to dependency group |
| Remove dependency |
| Install dependencies |
| Install all dependency groups |
| Run command in venv |
| Run with temporary dependency |
| Build package |
| Publish to PyPI |
| 命令 | 描述 |
|---|---|
| 创建新项目 |
| 创建可分发的包 |
| 添加依赖 |
| 添加到开发依赖组 |
| 移除依赖 |
| 安装依赖 |
| 安装所有依赖组 |
| 在虚拟环境中运行命令 |
| 临时添加依赖并运行命令 |
| 构建包 |
| 发布到PyPI |
--with--withuv run --withundefineduv run --withundefined
**When to use `--with` vs `uv add`:**
- `uv add`: Package is a project dependency (goes in pyproject.toml/uv.lock)
- `--with`: One-off usage, testing, or scripts outside a project context
See [uv-commands.md](./references/uv-commands.md) for complete reference.
**`--with`与`uv add`的使用场景区分:**
- `uv add`:包是项目的正式依赖(会写入pyproject.toml/uv.lock)
- `--with`:一次性使用、测试或项目外脚本场景
完整命令参考见[uv-commands.md](./references/uv-commands.md)。[dependency-groups]
dev = ["ruff", "ty"]
test = ["pytest", "pytest-cov", "hypothesis"]
docs = ["sphinx", "myst-parser"]uv sync --group dev --group test[dependency-groups]
dev = ["ruff", "ty"]
test = ["pytest", "pytest-cov", "hypothesis"]
docs = ["sphinx", "myst-parser"]uv sync --group dev --group testsrc/requires-python = ">=3.11"select = ["ALL"]uv.locksrc/requires-python = ">=3.11"select = ["ALL"]uv.lock