Skip to Content
Python for Excel (Chinese Edition), 2nd Edition
book

Python for Excel (Chinese Edition), 2nd Edition

by Felix Zumstein
May 2026
Intermediate
418 pages
5h 50m
Chinese
O'Reilly Media, Inc.
Content preview from Python for Excel (Chinese Edition), 2nd Edition

附录 A. uv 包管理器

第 2 章中,我指导您运行了uv sync,该命令负责安装 Python 以及本书中使用到的所有包。在本附录中,我将向您展示如何使用 uv 创建新项目,以及如何添加、移除和升级依赖项。我还将介绍--project选项,该选项允许您在项目目录之外进行操作。最后,我将简要概述您应该了解的其他包管理器。

先决条件

本附录假设你已按照第 2 章的说明安装了 uv。

创建新项目

要使用uv创建 新项目,请打开终端并切换到您希望创建项目文件夹的目录。为了便于本附录的说明,我们将项目命名为myproject,并将其创建在您的用户目录中:

终端(Windows)

cd %USERPROFILE%
uv init --python=3.14 myproject

终端(macOS)

cd ~
uv init --python=3.14 myproject

--python=3.14替换为您想要使用的 Python 版本。您可以通过运行uv python list 来获取可用 Python 版本的列表。

选择本地文件夹

请务必在本地机器的某个文件夹中创建 uv 项目。由于 uv 会将 Python 环境安装到项目文件夹内,若使用 OneDrive 或 Dropbox 等 Cloud 服务,虚拟环境将被上传至 Cloud 端,这会导致速度极慢。

现在进入myproject目录并同步环境:

终端(Windows 和 macOS)

cd myproject
uv sync

这将创建虚拟环境(.venv目录)和uv.lock文件——稍后我会详细说明它们。

在 VS Code 中打开项目前请先同步

在 VS Code 中打开项目之前,请在终端中运行 `uv sync` 命令,这有助于 VS Code 自动识别正确的虚拟环境,无需手动干预。

运行uv sync` 后,在 VS Code 中打开该项目(请注意,命令中的点表示当前目录):

终端(Windows 和 macOS)

code .

在 VS Code 资源管理器中,你会看到 uv 生成的几个文件:

这是包含虚拟环境的文件夹。关于虚拟环境的说明,请参阅第 2 章

此文件包含不会被版本控制系统 Git 追踪的文件列表(仅在安装了 Git 时生成)。

该文件根据第一个命令中的--python参数锁定了 Python 版本。如有需要,您可以手动编辑该文件,或通过运行uv python pin 3.14 进行修改。

main.py

这只是一个示例文件,因此您可以立即运行uv run main.py来查看是否一切正常。

pyproject.toml

这是一个 Python 使用的通用配置文件。uv 使用它来存储您的顶级依赖项。toml是一种专为配置文件设计的文件格式,与ini文件类似。

README.md

您可以在此处添加项目的描述。

uv.lock

uv 会在此处锁定每个包(包括子依赖项)的确切版本。当您执行uv sync` 时,系统将根据此文件安装依赖项。

既然我们已经创建了一个新项目,接下来让我们看看如何添加和 移除包!

添加和移除包

您可以使用 `uv add` 添加依赖项(即包)。例如,要添加 Requests 和 xlwings,请运行以下命令:

终端(Windows 和 macOS)

uv add requests xlwings

运行此命令将执行以下操作:

  • 将依赖项(即包)添加到pyproject.toml文件中的project.dependencies ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习勒索软件响应和恢复 (Chinese Edition)

学习勒索软件响应和恢复 (Chinese Edition)

W. Curtis Preston, Michael Saylor
Prometheus:快速入门,第二版

Prometheus:快速入门,第二版

Julien Pivotto, Brian Brazil

Publisher Resources

ISBN: 0642572396008