uv 是 Rust 编写的新一代极速 Python 环境/包管理工具,兼容 pip/venv 语法且速度提升 10-100 倍,以下是全场景命令汇总和避坑指南,覆盖环境创建、包管理、依赖锁定等核心场景,适配 Windows/macOS/Linux。另外,为了便于pip用户迁移,安装包时使用uv pip install,另外也推荐使用uv add 安装依赖,uv remove卸载依赖。

uv venv | uv venv | .venv 文件夹,自动匹配系统 Python | |
uv venv <路径> | uv venv .venv_ct | .venv_ct),避免同名覆盖 | |
uv venv --python <版本> | uv venv .venv_311 --python 3.11 | 3.8/3.10/3.12 等 | |
uv venv --system-site-packages | uv venv .venv --system-site-packages | ||
uv venv --clear <路径> | uv venv --clear .venv |
激活环境(Windows):
# 激活默认环境
.venv\Scripts\Activate
# 激活自定义环境
venv_test \Scripts\Activate
# 退出环境
deactivate
uv pip 完全兼容 pip 语法,且速度远快于 pip,核心命令:
uv pip install <包名> | uv pip install opencv-python==4.1.0 | ==/>=/<) | |
uv pip install -r requirements.txt | uv pip install -r requirements.txt | ||
uv pip install . | uv pip install . | ||
uv pip install --editable . | uv pip install -e . | ||
uv pip uninstall <包名> | uv pip uninstall opencv-python | -y 跳过确认:uv pip uninstall -y opencv-python | |
uv pip list | uv pip list | ||
uv pip show <包名> | uv pip show opencv-python | ||
uv pip freeze | uv pip freeze > requirements.txt | ||
uv pip cache purge | uv pip cache purge |
uv 支持生成锁文件(类似 poetry/pipenv),确保环境一致性:
uv lock | uv lock | ||
uv lock --requirements <文件> | uv lock --requirements requirements.txt | ||
uv sync | uv sync |
uv --version | uv --version | ||
uv config get | uv config get | ||
uv config set <键> <值> | uv config set registry.index-url https://pypi.tuna.tsinghua.edu.cn/simple | ||
uv cache dir | uv cache dir | C:\Users\<用户名>\AppData\Local\uv\cache | |
uv python list | uv python list | ||
uv python install <版本> | uv python install 3.10 |
初始化脚本项目
uv init --script analyze.py
添加脚本依赖
uv add pandas --script analyze.py
运行脚本(自动处理依赖)
uv run analyze.py
uv python list
#List the available Python installations
uv python install
#Download and install Python versions
uv python upgrade
#Upgrade installed Python versions
uv python find
#Search for a Python installation
uv python pin
#Pin to a specific Python version
uv python dir
#Show the uv Python installation directory
uv python uninstall
#Uninstall Python versions
uv python update-shell
#Ensure that the Python executable directory is on the PATH
.venv:若提示「环境已存在」,要么复用(选择现有解释器),要么改名创建(如 .venv_ct),不要直接覆盖。Set-ExecutionPolicy RemoteSigned -Scope CurrentUser,输入 Y 确认。.venv_ct(CT 图像处理)、.venv_ml(机器学习),便于区分。uv pip 和 pip:同一环境中用 uv 安装后,再用 pip 可能导致依赖冲突,全程用 uv pip。
pyproject.toml/setup.py,否则会报错。默认使用官方 PyPI,国内建议配置清华/阿里镜像:
# 临时使用(单次安装)
uv pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
# 全局配置(永久生效)
uv config set registry.index-url https://pypi.tuna.tsinghua.edu.cn/simple
uv python list 查看。uv python install <版本> 预安装。<环境目录>\Scripts\python.exe(Windows)。uv pip cache purge 即可。.venv_old),无残留(区别于 conda)。uv venv .venv_ct --python 3.11 | |
uv pip install opencv-python==4.1.0 | |
uv pip freeze > requirements.txt | |
uv venv .venv && uv pip install -r requirements.txt | |
uv config set registry.index-url https://pypi.tuna.tsinghua.edu.cn/simple |
uv venv 替代 python -m venv,uv pip 替代 pip,语法完全兼容且速度大幅提升;