OhMyIWB/source/_posts/0711_43.md
2026-07-12 00:02:52 +08:00

133 lines
4.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

------
title: "07/11 Vol.43 uv - Python 环境管理器食用指南"
date: 2026-07-10 23:00:00
tags: [Daily, Python, uv]
categories: [Daily]
------
初期写 Python 你可能还体会不到一个良好的环境管理器的作用,毕竟你可能只是全局 `pip install somemodule` 而了事。不过一当你装上 Python 版本多起来,即使是 `venv` 这样的虚拟环境管理器可能也有心无力。
**`uv`** 作为一款广受好评的包/项目管理器,既快速(~~神秘小螃蟹发力了~~)又强大(甚至不需要 Python 本体),由 Astral 出品,绝对值得信赖。
> 另外一提,他们家的 `ruff` 和 `ty` 也非常好用,有闲暇时间可以去看看:[Astral 官网](https://astral.sh)
## 下载与安装
uv 有超多安装方式。它在 PyPI 有售:
```sh
pipx install uv
```
它在 winget 有售:
```powershell
winget install --id=astral-sh.uv -e
```
它在 homebrew 有售:
```zsh
brew install uv
```
它在 crates.io 有售:(需要 Rust 工具链)
```sh
cargo install --locked uv
```
你可以使用独立安装脚本,对于 macOS / Linux
```sh
curl -LsSf https://astral.sh/uv/install.sh | sh
```
对于 Windows PowerShell
```powershell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
```
在 [安装 | uv](https://docs.astral.sh/uv/getting-started/installation/) 了解更多方式。
## 基本用途
### 创建项目
使用 uv 创建项目十分简便。以下部分内容来自官方教程。
使用 `uv init` 即可创建新项目:
```sh
uv init hello-world
cd hello-world
```
这样hello-world 目录就会出现如下所示的文件树:
```
├── .git/ ← Git 目录
├── .gitignore ← Git 忽略文件
├── .python-version ← Python 版本指示
├── README.md ← README 文档文件
├── main.py ← 代码
└── pyproject.toml ← 项目配置文件(通用格式)
```
使用 `uv run` 命令即可在当前环境运行脚本:
```sh
uv run main.py
```
现在你就可以开始编程了,也别忘记修改 `pyproject.toml` 来配置你的项目。有关此标准格式的更多说明,可以参阅 [官方 `pyproject.toml` 文档](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/)。
### 管理依赖
uv 对于管理依赖也是小菜一碟。
- 使用 `uv add somemodule` 即可添加 PyPI (或指定源)的模块(指定版本也只需写出 `somemodule==0.0.0` 即可,与原版没什么区别);
- `uv remove somemodule` 可以移除依赖;
-`uv lock --upgrade-package somemodule` 可以更新模块到最新的版本。
关于更多 uv 命令,你可以在 [这里](https://docs.astral.sh/uv/getting-started/features/#python-versions) 找到一个 Cheatsheet。
## 管理 Python 版本
没错现在你可以把你电脑里那些乱糟糟的蟒蛇都请出去了bushi紫外线会处理好一切
这样可以用 uv 全局安装一个 Python可随意指定版本
```sh
uv python install 3.12
```
也可以指定不同的 Python 实现更多请参阅文档PyPy 是用 Python **语言**写成的 Python **解译器**,最常见实现则是 C 语言实现的 CPython。
```sh
uv python install pypy@3.10
```
uv 会自动把它们添加到系统 Path 中,这意味着直接使用 `python3.12 ...` 即可使用安装好的 Python。
你也可以在 `uv init` 后用 `--python ...` 指定项目使用的 Python 版本。
## 为什么选它?
选 uv 的好处有很多:(摘自官网)(括号不是)
- 一个工具即可替代pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv...(如数家珍这一块)
- 比 pip 快 10-100 倍。(螃蟹发力中)
- 提供全面的项目管理,并具有通用 lock 文件。
- 运行脚本,支持内联依赖元数据。
- 安装和管理 Python 版本。
- 运行并安装以 Python 包形式发布的工具。
- 包含一个与 pip 兼容的接口,可在熟悉的命令行界面 (CLI) 上提升性能。
- 支持 Cargo 风格的工作空间适用于可扩展的项目。cargocargocargo💀
- 节省磁盘空间,并采用全局缓存进行依赖关系去重。(不知道为什么我这儿硬链接一直失效不过管他呢)
- 无需 Rust 或 Python 即可通过 curl 等方式安装。
- 支持 macOS、Linux 和 Windows。
总之,这篇文章只是很肤浅的说了一下 uv 的基本功能,了解更多?请自行前往官网。
> ~~没人觉得 OhMyIWB 与 IWB 越来越没有什么关系了吗~~哦草管他呢,你能看到 OMI 就是最大的幸运了不是吗(