uv 安装
开始之前:如果你偏好视频指引,可以通过这个视频了解如何进行 uv 的环境搭建。
默认情况下,官方给出的安装方式如下:
Windows(在 PowerShell 下执行):
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Linux/macOS:
curl -LsSf https://astral.sh/uv/install.sh | sh
如果你直接跑上面命令就成功安装了,那很好,请直接跳到“(二)给 uv 换国内源”
(一)使用镜像下载安装
因为资源实际上是存储在 Github 上的,因此在没有梯子的情况下,上面的代码可能跑不动。这时候,你可以参考下面的指引通过镜像来安装 uv:
Windows:
- 通过镜像 https://ghfast.top/https://github.com/astral-sh/uv/releases/latest/download/uv-installer.ps1 下载最新版uv的安装脚本。
- 用编辑器打开这个文件,然后将所有出现的 https://github.com 替换为 https://ghfast.top/https://github.com ,保存。
- 在 PowerShell 中打开当前目录,执行:
powershell -ExecutionPolicy Bypass -File .\uv-installer.ps1
Linux/macOS:
在 Linux 下操作原理相同,但我们可以直接通过 shell scripts 来执行:
# 1. 通过镜像下载脚本
curl -LsSf https://ghfast.top/https://github.com/astral-sh/uv/releases/latest/download/uv-installer.sh -o uv-installer.sh
# 2. 添加执行权限
chmod +x uv-installer.sh
# 3.1 替换文本,Linux 用这行
sed -i 's|https://github.com|https://ghfast.top/https://github.com|g' uv-installer.sh
# 3.2 替换文本,macOS 用这行
sed -i '' 's|https://github.com|https://ghfast.top/https://github.com|g' uv-installer.sh
# 4. 执行脚本安装
./uv-installer.sh
按顺序在终端输入上面 4 行命令。
如果镜像链接失效了,就需要再找了,但命令基本上不会变。如果实在安装不上的,可以在这里的评论通知我更新一下。
(二)给 uv 换国内源
用 uv 来安装 Python 或包的时候,如果不换源则会默认使用国外的源,速度会很慢。换源我们只需要两行代码:
index-url = "https://mirrors.cloud.tencent.com/pypi/simple/"
python-install-mirror = "https://registry.npmmirror.com/-/binary/python-build-standalone/"
第一行代码是用于替换 PYPI 包的下载源,而第二行则用于替换下载 Python 解释器的源。
在 Windows 下,将此文本保存为 uv.toml 并放置于 %APPDATA%/uv 目录下(你可以将目录直接粘贴到地址栏中跳转)。
在 Linux 下,也是保存为 uv.toml,然后放置于 ~/.config/uv 下即可。
