python uv包管理器使用

官方文档:uv官方文档

注:uv安装不依赖python。

使用:

python版本管理

shell 复制代码
# 查看已安装的python列表
uv python list
# 安装特定版本
uv python install 3.12
# 指定项目使用的python版本
uv python pin <version>

# 使用指定版本运行脚本
uv run --python 3.10 example.py

初始化项目

shell 复制代码
uv init hello-world
cd hello-world
shell 复制代码
mkdir hello-world
cd hello-world
uv init

包管理

shell 复制代码
# 安装包
uv add requests
# 升级包
uv lock --upgrade-package requests
# 卸载包
uv remove requests
# 手动同步包环境(uv run 会自动同步)
uv sync
# 升级所有包
uv lock --upgrade
# 添加httpx 从GitHub
uv add "httpx @ git+https://github.com/encode/httpx"
# 在 requirements.txt 文件中声明的依赖项
uv add -r requirements.txt
# 添加一个Git依赖源
uv add git+https://github.com/encode/httpx
uv add git+ssh://git@github.com/encode/httpx
uv add git+https://github.com/encode/httpx --branch main

运行命令

注:uv run 可以从dotenv文件中加载环境变量(例如,.env,.env.local, .env.development)

要禁用 dotenv 加载(例如,覆盖 UV_ENV_FILE 或命令行参数 --env-file),请将 UV_NO_ENV_FILE 环境变量设置为 1,或传递 --no-env-file 标志给 uv run。

shell 复制代码
uv run python -c "import example"
uv run example.py

工具

注:uvx别名 uv tool run --- 这两个命令是完全等价的

  1. 使用uv tool run在不安装的情况下调用,在这种情况下,它们的依赖项会安装在一个临时的、与当前项目隔离的虚拟环境中
  2. 工具也可以通过uv tool install安装,其可执行文件 可在PATH上获得 ------ 尽管使用的是隔离的虚拟环境,但该环境不会在命令完成时被移除。

缓存

shell 复制代码
# 移除所有缓存目录中的缓存条目
uv cache clean
# 移除所有缓存条目 ruff 的包
uv cache clean ruff
# 移除所有未使用的缓存条目(uv cache prune定期运行是安全的)
uv cache prune

相关参考:

【Python】uv 库:下一代开源的 Python 包管理工具

相关推荐
IVEN_10 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang11 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮11 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling12 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮15 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽15 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers