Python新包管理工具uv的使用

之前一直用的poetry,但是这个不能管理Python版本,需要使用pyenv,这个原生支持的是linux和mac,Windows版本虽然也有一个pyenv-win,但总的还是有点麻烦,无意中看到有个rust写的uv,速度是真的快,而且可以一键管理Python版本,很省事。

安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# On macOS and Linux.

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



# On Windows.

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"



# With pip.

pip install uv



# Or pipx.

pipx install uv

官方文档地址:https://docs.astral.sh/uv/

使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 查看可用的 Python 版本

uv python list



# 安装特定版本的 Python

uv python install 3.12



# 创建虚拟环境

uv venv



# 激活虚拟环境

source .venv/bin/activate # macOS/Linux

venv\Scripts\activate # Windows



# 为项目固定 Python 版本

uv python pin 3.11



# 安装包

uv pip install requests



# 升级包

uv pip upgrade requests



# 卸载包

uv pip uninstall requests



# 如果想全局系统级别使用,可以添加 --system参数:



uv pip install --system pandas



# 导出依赖

uv pip freeze > requirements.txt



# 初始化新项目

uv init my_project



# 安装项目依赖

uv sync



设置国内镜像源



[tool.uv]

index-url = "https://pypi.tuna.tsinghua.edu.cn/simple"
使用 Hugo 构建
主题 StackJimmy 设计