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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值