python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python依赖管理工具UV安装

python依赖管理工具UV的安装和使用教程

作者:qq_38608184

UV是一个用Rust编写的Python包安装和依赖管理工具,比传统工具(如 pip)有着更快、更高效的体验,这篇文章主要介绍了python依赖管理工具UV的安装和使用的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

前言

注:dify的插件是依赖uv的,对于dify1.0.0以及后续的版本,需要安装uv。

一、命令安装uv

官网安装步骤(https://docs.astral.sh/uv/getting-started/installation/)。运行以下其中一个命令安装uv,安装会受网速影响。

curl -LsSf https://astral.sh/uv/install.sh | sh

wget -qO- https://astral.sh/uv/install.sh | sh

curl -LsSf https://astral.sh/uv/0.7.12/install.sh | sh  # 安装指定版本的uv

以下是独立安装拆分步骤

# 1. 先下载脚本并检查
curl -LO https://astral.sh/uv/0.7.12/install.sh
# 2. 检查脚本内容
cat install.sh      # 或使用文本编辑器查看
# 3. 确认安全后执行
sh install.sh

二、手动编译安装

2.1在archlinux安装uv的依赖工具

sudo pacman -Syu base-devel git python python-pip rust

base-devel:提供 gcc、make 等编译工具。
git:用于克隆源码。
python 和 python-pip:uv 是 Python 工具,需要 Python 环境。
rust:uv 是用 Rust 编写的,需要 Rust 工具链。

2.2从Github网站获取源码

uv项目的Github网址https://github.com/astral-sh/uv/tree/0.7.12。直接下载zip源码,然后解压复制到archlinux中/root目录下。或者运行以下命令下载:

git clone https://github.com/astral-sh/uv.git
cd uv

git checkout tags/0.7.12  # 该步可选,切换到指定版本(如 0.7.12)

2.3 编译安装

编辑 Cargo 配置文件 ~/.cargo/config.toml,添加国内镜像源(如清华源或中科大源):

[source.crates-io]
replace-with = 'tuna'  # 或 'ustc'

# 清华大学镜像源
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

# 或中科大镜像源
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index/"

生效方式

​ 保存文件后,重新运行 cargo build --release

~/.cargo/config.toml 中增加超时时间(不推荐长期使用):

[net]
git-fetch-with-cli = true  # 使用系统 git 代替内置 git
timeout = 120              # 超时时间设为 120 秒

使用 cargo 构建

cargo build --release 

编译完成后,安装到指定目录

mkdir -p ~/.local/bin
cp target/release/uv ~/.local/bin/

cp target/release/uv /root/uv/uv-0.7.12

echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.profile
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bash_profile

exec "$SHELL"   # 重启shell,使设置的环境变量生效

2.4 验证安装

uv --version

应输出类似:

uv 0.7.12

2.5删除target

如果已经确认uv编译完成,可以删除target文件夹来释放存储空间

进入uv所在目录,查看uv依赖项
ldd ./uv

rm -r /root/uv/uv-0.7.12/target
尽量保留.so文件

2.6使用示例

$ uv init example
Initialized project `example` at `/home/user/example`

$ cd example

$ uv add ruff
Creating virtual environment at: .venv
Resolved 2 packages in 170ms
   Built example @ file:///home/user/example
Prepared 2 packages in 627ms
Installed 2 packages in 1ms
 + example==0.1.0 (from file:///home/user/example)
 + ruff==0.5.0

$ uv run ruff check
All checks passed!

$ uv lock
Resolved 2 packages in 0.33ms

$ uv sync
Resolved 2 packages in 0.70ms
Audited 1 package in 0.02ms

三、更改国内下载镜像

更改国内镜像地址,有以下两种方法,选择其中一种就行。推荐设置全局配置文件uv.toml

1.编辑uv配置文件

1.1全局配置文件(推荐)

在/etc/uv/目录下设置uv.toml配置文件。以下设置只能对uv安装python工具包起作用,即uv下载pypi工具时,会默认从设置的网站下载。但是对于python解释器的下载,需要另外设置,具体设置就是创建软链接,见为uv设置python解释器

mkdir /etc/uv

echo '
[[index]]
url = "https://mirrors.aliyun.com/pypi/simple/"
default = true'  > /etc/uv/uv.toml

1.2具体项目的配置文件

在uv目录下设置pyproject.toml配置文件。

vim pyproject.toml

增加以下内容
[[tool.uv.index]]
url = "https://mirrors.aliyun.com/pypi/simple/"

[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true

[tool.uv.pip]
index-url = "https://pypi.tuna.tsinghua.edu.cn/simple"

2.直接设置环境参数

有关uv的环境参数名称见官网https://docs.astral.sh/uv/reference/environment/#uv_break_system_packages

echo 'export UV_DEFAULT_INDEX="https://mirrors.aliyun.com/pypi/simple/"' >> ~/.bashrc
echo 'export UV_DEFAULT_INDEX="https://mirrors.aliyun.com/pypi/simple/"' >> ~/.profile
echo 'export UV_DEFAULT_INDEX="https://mirrors.aliyun.com/pypi/simple/"' >> ~/.bash_profile
exec "$SHELL"   # 重启shell,使设置的环境变量生效

四、为uv设置单独的虚拟环境

1.为uv设置python解释器

将pyenv安装的python作为uv的python解释器,创建软连接。uv会优先在环境变量中查找已经安装的python解释器,查找不到,才会从默认的网站下载。其默认的网站一般是国外网站,会因为网速问题导致uv下载python解释器失败。

ln -s  "$HOME/.pyenv/versions/3.12.6/bin/python"  /usr/local/bin/python3.12

2.创建独立的虚拟环境

使用 uv 直接创建并激活虚拟环境。**使用uv venv创建虚拟环境,会自动下载对应的python解释器到以下路径/root/.local/share/uv/python/下。**并且在/opt/dify-python/lib/uv-venv/bin目录中创建软链接指向自动下载的python解释器。

uv venv /path/to/uv-venv
source /path/to/uv-venv/bin/activate

示例:
uv venv /opt/dify-python/lib/uv-venv
source /opt/dify-python/lib/uv-venv/bin/activate

3.退出虚拟环境

deactivate

4.在独立环境中使用 uv

激活虚拟环境后,所有通过 uv pip install 安装的包将仅存在于(每个项目的) ./venv/lib/pythonX.Y/site-packages安装包(仅影响当前 uv-venv 环境)

uv pip install numpy openai

或者
uv pip install -r requirements.txt  # 安装项目依赖

5.与 poetry 环境完全隔离

poetry 环境:继续在项目目录中通过 poetry install 管理主依赖。

uv 环境:仅在手动激活 uv-venv 后操作,不影响 poetry。

验证隔离性:

检查当前环境的 Python 路径

which python

应输出 uv-venv 的路径,如:/path/to/uv-venv/bin/python

对比两个环境的包列表

在 uv-venv 中:
uv pip list -v   # 列出所有已安装的包及其版本

在 poetry 环境中:
poetry run pip list

总结 

到此这篇关于python依赖管理工具UV的安装和使用的文章就介绍到这了,更多相关python依赖管理工具UV安装内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文