uv-run
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUV Run
UV Run
Run Python scripts with uv - no manual venv activation needed.
使用uv运行Python脚本,无需手动激活虚拟环境。
Core Capabilities
核心功能
- Direct execution: handles environment automatically
uv run script.py - Temporary dependencies: for one-off needs
uv run --with requests script.py - Inline dependencies: PEP 723 metadata blocks for self-contained scripts
- Ephemeral tools: runs CLI tools without installation
uvx tool
- 直接执行:会自动处理环境
uv run script.py - 临时依赖:可满足一次性依赖需求
uv run --with requests script.py - 内联依赖:支持PEP 723元数据块,实现自包含脚本
- 临时工具:无需安装即可运行CLI工具
uvx tool
Essential Commands
常用命令
Running Scripts
运行脚本
bash
undefinedbash
undefinedRun script (uv manages environment automatically)
Run script (uv manages environment automatically)
uv run script.py
uv run script.py
Run with arguments
Run with arguments
uv run script.py --input data.csv --output results.json
uv run script.py --input data.csv --output results.json
Run a specific module
Run a specific module
uv run -m http.server 8000
uv run -m http.server 8000
Run with specific Python version
Run with specific Python version
uv run --python 3.12 script.py
undefineduv run --python 3.12 script.py
undefinedTemporary Dependencies
临时依赖
Add dependencies for a single run without modifying project:
bash
undefined单次运行时添加依赖,无需修改项目配置:
bash
undefinedSingle dependency
Single dependency
uv run --with requests fetch_data.py
uv run --with requests fetch_data.py
Multiple dependencies
Multiple dependencies
uv run --with requests --with rich api_client.py
uv run --with requests --with rich api_client.py
Version constraints
Version constraints
uv run --with 'requests>=2.31' --with 'rich>12,<14' script.py
uv run --with 'requests>=2.31' --with 'rich>12,<14' script.py
Combine with project dependencies
Combine with project dependencies
uv run --with pytest-benchmark pytest # Add benchmark to existing pytest
undefineduv run --with pytest-benchmark pytest # Add benchmark to existing pytest
undefinedInline Script Dependencies (PEP 723)
内联脚本依赖(PEP 723)
Create self-contained scripts with embedded dependencies:
python
#!/usr/bin/env -S uv run --script通过内嵌依赖创建自包含脚本:
python
#!/usr/bin/env -S uv run --script/// script
/// script
requires-python = ">=3.11"
requires-python = ">=3.11"
dependencies = [
dependencies = [
"requests>=2.31",
"requests>=2.31",
"rich>=13.0",
"rich>=13.0",
]
]
///
///
import requests
from rich import print
response = requests.get("https://api.github.com")
print(response.json())
Run directly:
```bash
uv run script.pyimport requests
from rich import print
response = requests.get("https://api.github.com")
print(response.json())
直接运行即可:
```bash
uv run script.pyOr make executable:
Or make executable:
chmod +x script.py
./script.py
undefinedchmod +x script.py
./script.py
undefinedManaging Script Dependencies
管理脚本依赖
bash
undefinedbash
undefinedInitialize script with metadata
Initialize script with metadata
uv init --script example.py --python 3.12
uv init --script example.py --python 3.12
Add dependency to script
Add dependency to script
uv add --script example.py requests rich
uv add --script example.py requests rich
Lock script dependencies for reproducibility
Lock script dependencies for reproducibility
uv lock --script example.py
undefineduv lock --script example.py
undefinedEphemeral Tool Execution (uvx)
临时工具执行(uvx)
Run CLI tools without installation:
bash
undefined无需安装即可运行CLI工具:
bash
undefinedRun tool once
Run tool once
uvx pycowsay "Hello from uv!"
uvx httpie https://api.github.com
uvx ruff check .
uvx pycowsay "Hello from uv!"
uvx httpie https://api.github.com
uvx ruff check .
With specific version
With specific version
uvx ruff@0.1.0 check .
uvx ruff@0.1.0 check .
uvx is shorthand for:
uvx is shorthand for:
uv tool run pycowsay "Hello"
undefineduv tool run pycowsay "Hello"
undefinedShebang Patterns
Shebang使用模式
Self-Contained Executable Script
自包含可执行脚本
python
#!/usr/bin/env -S uv run --scriptpython
#!/usr/bin/env -S uv run --script/// script
/// script
dependencies = ["click", "rich"]
dependencies = ["click", "rich"]
///
///
import click
from rich import print
@click.command()
@click.argument('name')
def hello(name):
print(f"[green]Hello, {name}![/green]")
if name == "main":
hello()
undefinedimport click
from rich import print
@click.command()
@click.argument('name')
def hello(name):
print(f"[green]Hello, {name}![/green]")
if name == "main":
hello()
undefinedScript with Python Version Requirement
指定Python版本要求的脚本
python
#!/usr/bin/env -S uv run --script --python 3.12python
#!/usr/bin/env -S uv run --script --python 3.12/// script
/// script
requires-python = ">=3.12"
requires-python = ">=3.12"
dependencies = ["httpx"]
dependencies = ["httpx"]
///
///
import httpx
import httpx
Uses Python 3.12+ features
Uses Python 3.12+ features
undefinedundefinedWhen to Use Each Pattern
各模式适用场景
| Scenario | Approach |
|---|---|
| Quick one-off task | |
| Reusable script | Inline deps (PEP 723) |
| Shareable utility | PEP 723 + shebang |
| Team collaboration | Inline deps + |
| Run CLI tool once | |
| Project scripts | |
| 场景 | 方案 |
|---|---|
| 快速一次性任务 | |
| 可复用脚本 | 内联依赖(PEP 723) |
| 可分享的工具脚本 | PEP 723 + shebang |
| 团队协作场景 | 内联依赖 + |
| 单次运行CLI工具 | |
| 项目脚本 | |
uvx vs uv run --with
uvx 与 uv run --with 的区别
- : Run a CLI tool without installation (e.g.,
uvx tool)uvx ruff check . - : Run a script with temporary dependencies (e.g.,
uv run --with pkg)uv run --with requests script.py
bash
undefined- :无需安装运行CLI工具(例如
uvx tool)uvx ruff check . - :带临时依赖运行脚本(例如
uv run --with pkg)uv run --with requests script.py
bash
undefinedRun a tool (CLI application)
Run a tool (CLI application)
uvx httpie https://api.github.com
uvx httpie https://api.github.com
Run a script with dependencies
Run a script with dependencies
uv run --with httpx api_test.py
undefineduv run --with httpx api_test.py
undefinedProfiling and Debugging
性能分析与调试
bash
undefinedbash
undefinedCPU profiling
CPU性能分析
uv run python -m cProfile -s cumtime script.py | head -20
uv run python -m cProfile -s cumtime script.py | head -20
Line-by-line profiling (temporary dependency)
逐行性能分析(临时依赖)
uv run --with line-profiler kernprof -l -v script.py
uv run --with line-profiler kernprof -l -v script.py
Memory profiling
内存分析
uv run --with memory-profiler python -m memory_profiler script.py
uv run --with memory-profiler python -m memory_profiler script.py
Real-time profiling (ephemeral tool)
实时性能分析(临时工具)
uvx py-spy top -- python script.py
uvx py-spy top -- python script.py
Quick profiling
快速性能分析
uv run --with scalene python -m scalene script.py
undefineduv run --with scalene python -m scalene script.py
undefinedSee Also
相关内容
- uv-project-management - Project dependencies and lockfiles
- uv-tool-management - Installing CLI tools globally
- python-development - Core Python language patterns
- uv-project-management - 项目依赖与锁文件
- uv-tool-management - 全局安装CLI工具
- python-development - Python核心语言使用模式