uv简单使用
通过uv创建项目和虚拟环境
初始化项目
uv init --package my-project
初始化一个名为 my-project
的新项目,并生成必要的文件结构。
创建虚拟环境
uv venv .venv
激活虚拟环境
# For Windows
.venv\Scripts\activate# For macOS/Linux
source .venv/bin/activate
确认虚拟环境已激活后,你会在命令行前端看到 (.venv) 的标识。
此时,项目目录结构应如下所示:
.
├── pyproject.toml
├── README.md
├── src
│ └── my-project
│ ├── __init__.py
src/my-project
:源代码目录,包含项目的 Python 模块
添加Python库
这里添加google的A2A库
uv add git+https://github.com/djsamseng/A2A#subdirectory=samples/python --branch prefixPythonPackage
uv add
:使用 UV 工具添加依赖。git+https://github.com/djsamseng/A2A#subdirectory=samples/python
:指定依赖的 Git 仓库和子目录。-branch prefixPythonPackage
:指定使用的分支
创建一些文件
touch src/my_project/agent.pytouch
touch src/my_project/task_manager.py
运行项目
uv run my-project
运行输出:
Hello from my-project!
uv换源
[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true[tool.uv]
python-install-mirror = "https://mirror.nju.edu.cn/github-release/indygreg/python-build-standalone"
分别加速下载依赖和下载python的速度,将其放在pyproject.toml中。
uv用于项目的指令
uv init
: Create a new Python project.uv add
: Add a dependency to the project.uv remove
: Remove a dependency from the project.uv sync
: Sync the project's dependencies with the environment.uv lock
: Create a lockfile for the project's dependencies.uv run
: Run a command in the project environment.uv tree
: View the dependency tree for the project.uv build
: Build the project into distribution archives.uv publish
: Publish the project to a package index.
uv文档地址
uv