uv
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseuv
uv
uv is an extremely fast Python package and project manager. It replaces pip,
pip-tools, pipx, pyenv, virtualenv, poetry, etc.
uv是一款速度极快的Python包与项目管理器。它可替代pip、pip-tools、pipx、pyenv、virtualenv、poetry等工具。
When to use uv
何时使用uv
Always use uv for Python work, especially if you see:
- The file
uv.lock - uv headers in files, e.g., "This file was autogenerated by uv"
requirements*
Don't use uv in projects managed by other tools:
- Poetry projects (identifiable by file)
poetry.lock - PDM projects (identifiable by file)
pdm.lock
Python开发工作中应优先使用uv,尤其是出现以下情况时:
- 存在文件
uv.lock - 文件中包含uv头部信息,例如:"This file was autogenerated by uv"
requirements*
请勿在由其他工具管理的项目中使用uv:
- Poetry项目(可通过文件识别)
poetry.lock - PDM项目(可通过文件识别)
pdm.lock
Choosing the right workflow
选择合适的工作流
Scripts
脚本场景
Use when: Running single Python files and standalone scripts.
Key commands:
bash
uv run script.py # Run a script
uv run --with requests script.py # Run with additional packages
uv add --script script.py requests # Add dependencies inline to the script适用场景: 运行单个Python文件或独立脚本。
核心命令:
bash
uv run script.py # 运行脚本
uv run --with requests script.py # 附加依赖包运行脚本
uv add --script script.py requests # 为脚本内联添加依赖Projects
项目场景
Use when: There is a or
pyproject.tomluv.lockKey commands:
bash
uv init # Create new project
uv add requests # Add dependency
uv remove requests # Remove dependency
uv sync # Install from lockfile
uv run <command> # Run commands in environment
uv run python -c "" # Run Python in project environment
uv run -p 3.12 <command> # Run with specific Python version适用场景: 项目中存在或文件
pyproject.tomluv.lock核心命令:
bash
uv init # 创建新项目
uv add requests # 添加依赖
uv remove requests # 移除依赖
uv sync # 根据锁文件安装依赖
uv run <command> # 在环境中运行命令
uv run python -c "" # 在项目环境中运行Python代码
uv run -p 3.12 <command> # 使用指定Python版本运行命令Tools
工具场景
Use when: Running command-line tools (e.g., ruff, ty, pytest) without
installation.
Key commands:
bash
uvx <tool> <args> # Run a tool without installation
uvx <tool>@<version> <args> # Run a specific version of a toolImportant:
- runs tools from PyPI by package name. This can be unsafe - only run well-known tools.
uvx - Only use only when specifically requested by the user.
uv tool install
适用场景: 无需安装即可运行命令行工具(如ruff、ty、pytest)。
核心命令:
bash
uvx <tool> <args> # 无需安装直接运行工具
uvx <tool>@<version> <args> # 运行工具的指定版本注意事项:
- 通过包名称从PyPI运行工具,存在安全风险——仅运行知名工具。
uvx - 仅在用户明确要求时使用。
uv tool install
Pip interface
Pip兼容接口
Use when: Legacy workflows with or manual environment
management, no present.
requirements.txtuv.lockKey commands:
bash
uv venv
uv pip install -r requirements.txt
uv pip compile requirements.in -o requirements.txt
uv pip sync requirements.txt适用场景: 依赖的遗留工作流或手动环境管理,且无文件。
requirements.txtuv.lock核心命令:
bash
uv venv
uv pip install -r requirements.txt
uv pip compile requirements.in -o requirements.txt
uv pip sync requirements.txtPlatform independent resolution
跨平台解析
uv pip compile --universal requirements.in -o requirements.txt
**Important:**
- Don't use the pip interface unless clearly needed.
- Don't introduce new `requirements.txt` files.
- Prefer `uv init` for new projects.uv pip compile --universal requirements.in -o requirements.txt
**注意事项:**
- 除非明确需要,否则不要使用pip兼容接口。
- 不要新建`requirements.txt`文件。
- 新项目优先使用`uv init`。Migrating from other tools
从其他工具迁移
pyenv → uv python
pyenv → uv python
bash
pyenv install 3.12 → uv python install 3.12
pyenv versions → uv python list --only-installed
pyenv local 3.12 → uv python pin 3.12
pyenv global 3.12 → uv python install 3.12 --defaultbash
pyenv install 3.12 → uv python install 3.12
pyenv versions → uv python list --only-installed
pyenv local 3.12 → uv python pin 3.12
pyenv global 3.12 → uv python install 3.12 --defaultpipx → uvx
pipx → uvx
bash
pipx run ruff → uvx ruff
pipx install ruff → uv tool install ruff
pipx upgrade ruff → uv tool upgrade ruff
pipx list → uv tool listbash
pipx run ruff → uvx ruff
pipx install ruff → uv tool install ruff
pipx upgrade ruff → uv tool upgrade ruff
pipx list → uv tool listpip and pip-tools → uv pip
pip 和 pip-tools → uv pip
bash
pip install package → uv pip install package
pip install -r req.txt → uv pip install -r req.txt
pip freeze → uv pip freeze
pip-compile req.in → uv pip compile req.in
pip-sync req.txt → uv pip sync req.txt
virtualenv .venv → uv venvbash
pip install package → uv pip install package
pip install -r req.txt → uv pip install -r req.txt
pip freeze → uv pip freeze
pip-compile req.in → uv pip compile req.in
pip-sync req.txt → uv pip sync req.txt
virtualenv .venv → uv venvCommon patterns
通用规范
Don't use pip in uv projects
不要在uv项目中使用pip
bash
undefinedbash
undefinedBad
错误做法
pip install requests
pip install requests
Good
正确做法
uv add requests
undefineduv add requests
undefinedDon't run python directly
不要直接运行python
bash
undefinedbash
undefinedBad
错误做法
python script.py
python script.py
Good
正确做法
uv run script.py
```bashuv run script.py
```bashBad
错误做法
python -c "..."
python -c "..."
Good
正确做法
uv run python -c "..."
```bashuv run python -c "..."
```bashBad
错误做法
python3.12 -c "..."
python3.12 -c "..."
Good
正确做法
uvx python@3.12 -c "..."
undefineduvx python@3.12 -c "..."
undefinedDon't manually manage environments in uv projects
不要在uv项目中手动管理环境
bash
undefinedbash
undefinedBad
错误做法
python -m venv .venv
source .venv/bin/activate
python -m venv .venv
source .venv/bin/activate
Good
正确做法
uv run <command>
undefineduv run <command>
undefinedDocumentation
文档参考
For detailed information, read the official documentation:
The documentation links to specific pages for each of these workflows.