在这里插入图片描述

安装UV

【UV官方文档】
【UV中文文档】
首先到UV的 【UV的GIT地址】,然后下载一下最新版本即可。
将其解压在你喜欢的位置,记得不要有中文和空格之类的(老生常谈的东西了)。

配置UV的一些环境变量

环境变量名 作用
UV_CACHE_DIR D:/Enivronment/uv/uv_cache UV的缓存路径
UV_PYTHON_INSTALL_DIR D:/Enivronment/uv/uv_python UV的python解释器版本下载后放置的位置
UV_PYTHON_INSTALL_MIRROR https://registry.npmmirror.com/-/binary/python-build-standalone UV下载python解释器的地址
UV_TOOL_DIR D:/Enivronment/uv/uv_tools UV的Tool工具的存放路径
UV_HOME D:/Enivronment/uv UV的路径,需要加入到环境变量的PATH中 %UV_HOME%(windows下)

注意:这些配置不是必须配置的,使用默认配置也可以的。当然在国内的话,配置个UV_PYTHON_INSTALL_MIRROR还是有必要的,能够加快下载的速度。

检查UV是否安装成功

重新打开终端,输入以下命令

PS D:\python_workspace\pytorch-study> uv -V

uv 0.9.18 (0cee76417 2025-12-16)

初始化项目

创建一个新项目

# 你可以使用 uv init 命令创建一个新的 Python 项目:
uv init hello-world
cd hello-world

# 也可以自己创建好文件夹
mkdir hello-world
cd hello-world
uv init

# 如果你要指定python的版本
uv init -p 3.12

uv将会创建以下文件

├── .gitignore
├── .python-version
├── README.md
├── main.py
└── pyproject.toml

UV安装python版本

安装和管理 Python 本身。

uv python install:  安装 Python 版本。
uv python list:     查看可用的 Python 版本。
uv python find:     查找已安装的 Python 版本。
uv python pin:      将当前项目固定为使用特定的 Python 版本。
uv python uninstall:卸载 Python 版本。

如果你没有自己安装,uv会自行下载你指定的版本的python的。

UV安装python包

使用命令

# 安装包

uv add xxxxx

执行完成之后,就会将该依赖放入到pyproject.toml配置文件中的dependencies列表中。

UV进行项目级别的配置

主要在项目中的pyproject.toml配置文件中进行修改。

[project]
name = "pytorch-study"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "torch==2.7.0+cu128",
    "torchvision==0.22.0+cu128",
    "torchaudio==2.7.0+cu128",
    "xformers==0.0.30",
]

# 通过在 tool.uv.sources 中指定索引,可以将软件包固定到特定索引。例如,要确保 torch 总是从 pytorch 索引安装
[tool.uv.sources]
torch = { index = "pytorch-cu128" }
torchvision = { index = "pytorch-cu128" }
torchaudio = { index = "pytorch-cu128" }
xformers = { index = "pytorch-cu128" }

# 定义索引
[[tool.uv.index]]
# 索引的可选名称。
name = "pytorch-cu128"
# 索引的必需 URL。
url = "https://download.pytorch.org/whl/cu128"
# 一个索引可以被标记为 explicit = true,以防止软件包从该索引安装,除非明确固定到它
explicit = true

[[tool.uv.index]]
name = "pytorch-cu126"
url = "https://download.pytorch.org/whl/cu126"
explicit = true

[[tool.uv.index]]
name = "pytorch-cu124"
url = "https://download.pytorch.org/whl/cu124"
explicit = true

[[tool.uv.index]]
name = "llama-cpp"
url = "https://github.com/JamePeng/llama-cpp-python/releases"
explicit = true

[[tool.uv.index]]
name = "pypi"
url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
explicit = false
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐