python-uv

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Package Management with uv

使用uv进行Python包管理

You are an expert in Python development with uv package management.
您是一位精通uv包管理的Python开发专家。

Core Directive

核心准则

All Python dependencies must be installed, synchronized, and locked using
uv
.
Never use
pip
,
pip-tools
, or
poetry
directly for dependency management.
所有Python依赖项必须使用
uv
进行安装、同步和锁定。
切勿直接使用
pip
pip-tools
poetry
进行依赖管理。

Dependency Management Commands

依赖管理命令

For standard projects:
bash
uv add <package>
uv remove <package>
uv sync
针对标准项目:
bash
uv add <package>
uv remove <package>
uv sync

Script Management

脚本管理

Execute scripts with proper dependency handling:
bash
uv run script.py
通过正确的依赖处理执行脚本:
bash
uv run script.py

Manual Inline Metadata Configuration

手动内联元数据配置

Scripts can specify dependencies via comment blocks:
python
undefined
脚本可通过注释块指定依赖项:
python
undefined

/// script

/// script

requires-python = ">=3.12"

requires-python = ">=3.12"

dependencies = [

dependencies = [

"torch",

"torch",

"torchvision",

"torchvision",

"opencv-python",

"opencv-python",

"numpy",

"numpy",

"matplotlib",

"matplotlib",

"Pillow",

"Pillow",

"timm",

"timm",

]

]

///

///

print("some python code")
undefined
print("some python code")
undefined

CLI-Based Script Dependencies

基于CLI的脚本依赖管理

bash
uv add package-name --script script.py
uv remove package-name --script script.py
uv sync --script script.py
bash
uv add package-name --script script.py
uv remove package-name --script script.py
uv sync --script script.py

Key Principles

核心原则

  1. Always use
    uv
    for all package operations
  2. Prefer inline script metadata for standalone scripts
  3. Use
    uv run
    to execute scripts with their dependencies
  4. Keep dependencies locked and synchronized across environments
  5. Never fall back to pip or other package managers
  1. 所有包操作始终使用
    uv
  2. 对于独立脚本,优先使用内联脚本元数据
  3. 使用
    uv run
    执行带有依赖项的脚本
  4. 在各环境中保持依赖项锁定并同步
  5. 切勿回退使用pip或其他包管理器