安装
[[macOS]] 和 [[Linux]] 通过 [[cURL]] 安装:
curl -LsSf https://astral.sh/uv/install.sh | sh
macOS 还支持通过 [[Homebrew]] 安装:
brew install uv
uv 默认安装位置是:~/.cargo/bin,如果需要修改安装路径,可以配置 UV_INSTALL_DIR 变量:
curl -LsSf https://astral.sh/uv/install.sh | env UV_INSTALL_DIR="/custom/path" sh
卸载
brew uninstall uv
删除对应目录
rm ~/.cargo/bin/uv ~/.cargo/bin/uvx
使用
从项目开始
# 创建项目uv init <project_name>
# 在已有项目中初始化cd <project_name>uv init
初始化项目时,uv 可以指定两种类型:
- 应用程序:
init --app
(默认) - 库(包):
init --lib
依赖
# 添加uv add fastapi
# 删除uv remove fastapi
同步依赖
uv pip sync requirements.txt
命令
Python 安装和管理
- uv python
- install: 安装 Python 版本
- list: 查看可用的
- find: 查找已安装的
- uninstall: 卸载
- pin: 固定
pip 管理
- uv venv: 创建虚拟环境
- uv pip
- install: 安装软件包
- show: 显示已安装软件包信息
- list: 列出已安装软件包
- freeze: 列出已安装和对应版本
- uninstall: 卸载软件包
- tree: 查看依赖树
- sync: 将环境与锁定文件同步
项目管理
- uv init: 创建新的项目
- uv add: 添加依赖
- uv remove: 删除依赖
- uv sync: 从配置同步依赖到环境
- uv tree: 查看依赖关系树
运行脚本
- uv run: 运行 Python
- uv add: 向脚本添加依赖项
- uv remove: 从脚本移除依赖项
Docker 部署
使用 [[Dockerfile]] 部署使用 uv 管理的项目:
FROM python:3.12-slim
# Install uv.COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
# Copy the application into the container.COPY . /app
# Install the application dependencies.WORKDIR /appRUN uv sync --frozen --no-cache
# Run the application.CMD ["/app/.venv/bin/fastapi", "run", "app/main.py", "--port", "80", "--host", "0.0.0.0"]