Skip to content

使用 uv 来开发和管理 Python 程序

Published: at 00:00

说明

开发和运行一个 Python 程序通常涉及两个关键问题:选择 Python 版本管理程序依赖

uv 是一款新兴的 Python 依赖管理工具,能够替代这些传统工具,实现更高效的开发体验。它采用 Rust 编写,具备卓越的性能,同时整合了Python 版本管理依赖管理功能,无需手动创建虚拟环境,能够自动管理项目所需的环境。相比传统方案,uv 解析依赖的速度更快、使用方式更简单,是现代 Python 开发者的更理想选择。本文记录和介绍一下uv的安装和主要用法。

安装

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

使用

Python 版本

# 显示所有的可安装的 Python 版本
$ uv python list
cpython-3.14.0a5+freethreaded-macos-aarch64-none    <download available>
cpython-3.14.0a5-macos-aarch64-none                 <download available>
cpython-3.13.2+freethreaded-macos-aarch64-none      <download available>
cpython-3.13.2-macos-aarch64-none                   <download available>
cpython-3.13.1-macos-aarch64-none                   <download available>
cpython-3.12.9-macos-aarch64-none                   <download available>
cpython-3.12.8-macos-aarch64-none                   <download available>
cpython-3.11.11-macos-aarch64-none                  <download available>
cpython-3.11.11-macos-aarch64-none                  <download available>
cpython-3.11.5-macos-aarch64-none                   <download available>
cpython-3.11.5-macos-aarch64-none                   <download available>
cpython-3.11.5-macos-aarch64-none                   <download available>
cpython-3.10.16-macos-aarch64-none                  <download available>
cpython-3.10.16-macos-aarch64-none                  <download available>
cpython-3.9.21-macos-aarch64-none                   <download available>
cpython-3.9.21-macos-aarch64-none                   <download available>
cpython-3.9.6-macos-aarch64-none                    <download available>
cpython-3.8.20-macos-aarch64-none                   <download available>
pypy-3.11.11-macos-aarch64-none                     <download available>
pypy-3.10.16-macos-aarch64-none                     <download available>
pypy-3.9.19-macos-aarch64-none                      <download available>
pypy-3.8.16-macos-aarch64-none                      <download available>


# 安装指定版本(3.12.9)
$ uv python install cpython-3.12.9
Installed Python 3.12.9 in 29.76s
 + cpython-3.12.9-macos-aarch64-none

使用

指定 Python 版本运行
# 指定版本运行 Python 脚本(a.py)
$ uv run -p 3.12 a.py
Hello uv

$ uv run -p 3.12 python
Python 3.12.9 (main, Feb 12 2025, 15:09:19) [Clang 19.1.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
创建项目
# 创建并初始化项目工程
$ mkdir uv_app
$ cd uv_app
$ uv init -p 3.12
Initialized project `uv-app`

# 自动创建的文件
$ tree .
.
├── README.md
├── main.py
└── pyproject.toml

1 directory, 3 file
依赖包
# 安装指定依赖包
$ uv add requests
Using CPython 3.12.9
Creating virtual environment at: .venv
Resolved 6 packages in 2.41s
Prepared 5 packages in 1.84s
Installed 5 packages in 20ms
 + certifi==2025.1.31
 + charset-normalizer==3.4.1
 + idna==3.10
 + requests==2.32.3
 + urllib3==2.3.0

# 显示所有依赖关系
$ uv tree
Resolved 6 packages in 7ms
uv-app v0.1.0
└── requests v2.32.3
    ├── certifi v2025.1.31
    ├── charset-normalizer v3.4.1
    ├── idna v3.10
    └── urllib3 v2.3.0

# 安装指定版本的依赖包
$ uv add "requests==2.31.0"
Resolved 6 packages in 1.94s
Prepared 1 package in 1.09s
Uninstalled 1 package in 7ms
Installed 1 package in 8ms
 - requests==2.32.3
 + requests==2.31.0

# 删除指定依赖包
$ uv remove requests
Resolved 1 package in 13ms
Uninstalled 5 packages in 7ms
 - certifi==2025.1.31
 - charset-normalizer==3.4.1
 - idna==3.10
 - requests==2.32.3
 - urllib3==2.3.0
开发依赖包
# 安装指定开发依赖包(避免打包进来)
$ uv add ruff --dev
Resolved 2 packages in 2.32s
Prepared 1 package in 7.36s
Installed 1 package in 6ms
 + ruff==0.9.10


# 删除开发依赖包
$ uv remove ruff --group dev
Resolved 1 package in 9ms
Uninstalled 1 package in 1ms
 - ruff==0.9.10
工具
# 安装工具到系统中
$ uv tool install ruff
Resolved 1 package in 855ms
Installed 1 package in 11ms
 + ruff==0.9.10
Installed 1 executable: ruff

# 查看已安装的工具
$ uv tool list
ruff v0.9.10
- ruff
运行
# 运行脚本
$ uv run main.py
Hello from uv-app!
打包、安装和运行
# 编写 project.toml 增加以下信息
[project.scripts]
uv_app = "main:main"
# Build
$ uv build
Building source distribution...
running egg_info
creating uv_app.egg-info
writing uv_app.egg-info/PKG-INFO
writing dependency_links to uv_app.egg-info/dependency_links.txt
writing entry points to uv_app.egg-info/entry_points.txt
writing top-level names to uv_app.egg-info/top_level.txt
writing manifest file 'uv_app.egg-info/SOURCES.txt'
reading manifest file 'uv_app.egg-info/SOURCES.txt'
writing manifest file 'uv_app.egg-info/SOURCES.txt'
...
...
...
adding 'main.py'
adding 'uv_app-0.1.0.dist-info/METADATA'
adding 'uv_app-0.1.0.dist-info/WHEEL'
adding 'uv_app-0.1.0.dist-info/entry_points.txt'
adding 'uv_app-0.1.0.dist-info/top_level.txt'
adding 'uv_app-0.1.0.dist-info/RECORD'
removing build/bdist.macosx-11.0-arm64/wheel
Successfully built dist/uv_app-0.1.0.tar.gz
Successfully built dist/uv_app-0.1.0-py3-none-any.whl
# 安装
$ uv tool install dist/uv_app-0.1.0-py3-none-any.whl
Resolved 1 package in 10ms
Prepared 1 package in 17ms
Installed 1 package in 3ms
 + uv-app==0.1.0 (from file:///Users/hwsdien/uv_test/uv_app/dist/uv_app-0.1.0-py3-none-any.whl)
Installed 1 executable: uv_app

# 运行
$ uv_app
Hello from uv-app!

总结

uv 提供了一个高效、简单的方式来管理 Python 项目的版本和依赖,适合现代 Python 开发者使用。通过整合 Python 版本管理和依赖管理功能,uv 能够显著提升开发体验。