uv 是一个极其快速的 Python 包和项目管理器,用 Rust 编写。
其理念是将库都安装到本地,而不是环境中,然后对于每个项目有其特定的环境,项目所需要的库是由 hardlink 映射过来,就不用重新下载,这样就避免了重复安装库
安装
在终端运行
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
使用步骤
一:初始化
运行指令:
uv init --bare
其就会创建一个 pyproject.toml,里面会记录一些信息以及所需要用到的库
二:准备措施
如果要安装 torch,将以下内容复制到 pyproject.toml 之中,这是让其自动安装符合的 torch 版本
[tool.uv.sources]
torch = [
{ index = "pytorch-cu128", marker = "sys_platform == 'linux'" },
{ index = "pytorch-cu128", marker = "sys_platform == 'win32'" }
]
torchvision = [
{ index = "pytorch-cu128", marker = "sys_platform == 'linux'" },
{ index = "pytorch-cu128", marker = "sys_platform == 'win32'" }
]
xformers = [
{ index = "pytorch-cu128", marker = "sys_platform == 'linux'" },
{ index = "pytorch-cu128", marker = "sys_platform == 'win32'" }
]
[[tool.uv.index]]
name = "pytorch-cu124"
url = "https://download.pytorch.org/whl/cu124"
explicit = true
[[tool.uv.index]]
name = "pytorch-cu126"
url = "https://download.pytorch.org/whl/cu126"
explicit = true
[[tool.uv.index]]
name = "pytorch-cu128"
url = "https://download.pytorch.org/whl/cu128"
explicit = true
三:安装/引入所需的库
执行指令:uv add 后面接库的名字
库的名字的搜索方法:浏览器搜索 numpy pypi,然后出来的 pip install numpy 指令,其 numpy 就是它的名字,接在 add 后即可
这样安装/引入的好处是其能自动写到 pyproject.toml 之中
uv add numpy
uv pip install torch —torch-backend auto
四:复用pyproject.toml
如果这个项目已经有了 pyproject.toml 这种文件,那么直接运行以下命令就可以自动全部安装/引入好
uv sync