uv 管理工具

2025/12/02

安装开发依赖

uv sync --dev

这会将项目的开发依赖也一并安装到当前的虚拟环境中。

安装第三方仓库

如果需要在自己的项目下安装仓库类包:

uv add "git+https://github.com/user/repo.git"

但若该仓库不用 uv 管理,该命令通常不起作用。为了更好的利用 uv,应先在本项目的 pyproject.toml 文件中添加以下内容:

dependencies = [
    "第三方库名称"
]

[tool.uv.sources]
第三方库名称 = { path = "third_party/第三方库名称", editable = true }

其中的第三方库名称以代码中实际导入的名称为准。在本项目下创建 third_party 目录,执行:

git submodule add "https://github.com/user/repo.git" third_party/第三方库名称

让人抓狂的 --no-build-isolation

比如 flash-attn 库,就需要 torch 才能构建,因此通常会在安装的时候报错,并指示你使用 --no-build-isolation。但我们已经使用了 uv,自然有更高级的做法:

[tool.uv.extra-build-dependencies]
flash-attn = ["torch==2.2.0"]

只需要添加上这一行(注意版本号),uv 在构建 flash-attn 时就会自动等待 torch 的安装完成,再来构建 flash-attn。如果还是有问题,可以参考 Support flash attention flash-attn –no-build-isolation with uv sync