官方文档: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://[email protected]/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 --- 这两个命令是完全等价的
- 使用uv tool run在不安装的情况下调用,在这种情况下,它们的依赖项会安装在一个临时的、与当前项目隔离的虚拟环境中
- 工具也可以通过uv tool install安装,其可执行文件 可在PATH上获得 ------ 尽管使用的是隔离的虚拟环境,但该环境不会在命令完成时被移除。
缓存
shell
# 移除所有缓存目录中的缓存条目
uv cache clean
# 移除所有缓存条目 ruff 的包
uv cache clean ruff
# 移除所有未使用的缓存条目(uv cache prune定期运行是安全的)
uv cache prune