介绍uv
一个用 Rust 编写的极快的 Python 包和项目管理器,显示带有基准测试结果的条形图,使用热缓存安装Trio的依赖项。uv是一个python虚拟环境管理工具,可以用来替代pip、pyenv、virtualenv等等工具。根据官网的介绍,使用uv来管理虚拟环境,相比于pip能得到至少10倍以上的性能提升。亮点
- 🚀一个工具即可替换pip、、、、、、、等等pip-tools。pipxpoetrypyenvtwinevirtualenv⚡️比快 10-100 倍pip。🗂️ 提供全面的项目管理,并带有 通用的锁文件。❇️运行脚本,支持 内联依赖元数据。🐍安装和管理Python 版本。🛠️运行并安装作为 Python 包发布的工具。🔩 包含与pip 兼容的接口,可通过熟悉的 CLI 提高性能。🏢 支持可扩展项目的Cargo 风格工作区。💾 节省磁盘空间,具有用于依赖性重复数据删除的全局缓存。⏬ 无需 Rust 或 Python 即可通过curl或安装pip。🖥️ 支持 macOS、Linux 和 Windows。uv 得到了Ruff的创造者 Astral的支持。
uv工具有如下功能:
- 管理python版本;= 管理第三方库(Python packages)的版本;拥有全局的第三方库的缓存,能减少磁盘空间占用;安装uv不需要python环境,可以通过curl或pip安装;多平台支持:macOS、Linux、Windows;
UV环境安装
Linux、MAC
执行以下命令安装
``` bashcurl -LsSf https://astral.sh/uv/install.sh | sh或者wget -qO- https://astral.sh/uv/install.sh | sh```
按照提示设置环境变量
```bashdownloading uv 0.7.8 aarch64-apple-darwinno checksums to verifyinstalling to /Users/test/.local/binuvuvxeverything's installed!To add $HOME/.local/bin to your PATH, either restart your shell or run: source $HOME/.local/bin/env (sh, bash, zsh) source $HOME/.local/bin/env.fish (fish)WARN: The following commands are shadowed by other commands in your PATH: uv uvx```
windos
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
基本使用
# 查找python! uv python find# python列表! uv python list# Python 3.13 解释器! uv python list 3.13# 安装python! uv python install 3.10# 卸载指定的版本! uv python uninstall 3.10
uv创建项目
已有项目
➜ cd (uv_test)➜ uv init . -p 3.11.9
新项目
➜ uv init (uv_test) -p 3.11.9
同步项目依赖
➜ (uv_test) uv syncUsing CPython 3.11.9Creating virtual environment at: .venvResolved 1 package in 9msAudited in 0.19ms
激活虚拟环境
➜ (uv_test) source .venv/bin/activate(uv-test) ➜ (uv_test)
运行
➜ (uv_test) uv run main.pyHello from uv-test!
安装python包
(uv-test) ➜ (uv_test) uv add langchainResolved 28 packages in 4.94sPrepared 17 packages in 4.09sInstalled 28 packages in 51ms+ annotated-types==0.7.0+ anyio==4.9.0+ certifi==2025.4.26
查看当前项目依赖
(uv-test) ➜ (uv_test) uv tree Resolved 18 packages in 0.65msuv-test v0.1.0├── cantools v40.2.2│ ├── argparse-addons v0.12.0│ ├── bitstruct v8.21.0│ ├── crccheck v1.3.0│ ├── diskcache v5.6.3│ ├── python-can v4.5.0│ │ ├── msgpack v1.1.0│ │ ├── packaging v25.0│ │ ├── typing-extensions v4.13.2│ │ └── wrapt v1.17.2│ └── textparser v0.24.0└── pandas v2.2.3 ├── numpy v2.2.6 ├── python-dateutil v2.9.0.post0 │ └── six v1.17.0 ├── pytz v2025.2 └── tzdata v2025.2
卸载依赖
(uv-test) ➜ (uv_test) uv remove pandasResolved 12 packages in 6msUninstalled 32 packages in 290ms- annotated-types==0.7.0- anyio==4.9.0- certifi==2025.4.26- charset-normalizer==3.4.2- h11==0.16.0
修改源
pypi添加清华源
打开项目中的pyproject.toml文件,在最后添加以下内容:
[[index]]url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/"default = true
运行uv add命令的时候也可以指定镜像源
uv add --default-index https://pypi.tuna.tsinghua.edu.cn/simple requests
uv也提供了全局的配置项,可以通过环境变量UV_DEFAULT_INDEX配置镜像源
export UV_DEFAULT_INDEX=https://pypi.tuna.tsinghua.edu.cn/simple
本文由博客一文多发平台 OpenWrite 发布!