外观
uv 换国内源加速 — 比 pip 快 10 倍的包管理器配置
uv 是 Astral 出品的 Rust 版 Python 包管理器,解析和安装速度比 pip 快一个数量级,正在快速取代 pip / pip-tools / virtualenv。但默认源仍是 PyPI 官方,国内需要换源才能发挥全部速度。
方法一:环境变量(最快见效)
bash
# Linux / macOS(写入 ~/.bashrc 或 ~/.zshrc)
export UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple"powershell
# Windows PowerShell(持久化)
[Environment]::SetEnvironmentVariable('UV_DEFAULT_INDEX', 'https://pypi.tuna.tsinghua.edu.cn/simple', 'User')方法二:全局配置文件(推荐)
写入 uv 全局配置,一次配置所有项目生效:
- Linux / macOS:
~/.config/uv/uv.toml - Windows:
%APPDATA%\uv\uv.toml
toml
[[index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true常用国内镜像任选其一:
text
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:https://mirrors.aliyun.com/pypi/simple
中科大:https://mirrors.ustc.edu.cn/pypi/simple
腾讯云:https://mirrors.cloud.tencent.com/pypi/simple方法三:项目级配置(pyproject.toml)
只对当前项目生效,适合需要跟仓库一起分发的场景:
toml
[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true验证是否生效
bash
# -v 会打印实际请求的 index 地址
uv pip install requests -v 2>&1 | grep -i index或者直接感受速度 —— 换源后 uv pip install torch 应该能跑满带宽。
Python 解释器下载也要加速
uv python install 3.12 默认从 GitHub Releases 下载 python-build-standalone,国内经常超时。两种解决:
bash
# 方案 A:让下载走代理(把 7890 换成你的端口)
export https_proxy=http://127.0.0.1:7890
uv python install 3.12
# 方案 B:指定镜像地址(以镜像站实际路径为准)
export UV_PYTHON_INSTALL_MIRROR="https://ghproxy.net/https://github.com/indygreg/python-build-standalone/releases/download"
uv python install 3.12uv 也接管了 pip 的场景
uv pip install 兼容 pip 命令行参数,临时指定源可以用: uv pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple