一、开发环境安装
1.1 Windows环境安装步骤
【方法一】使用PowerShell安装(推荐)
# 使用官方安装脚本powershell -c "irm https://astral.sh/uv/install.ps1 | iex"# 或者使用pip(如果已有Python环境)pip install uv
- 下载 uv-x86_64-pc-windows-msvc.zip
# 解压到目录,例如 C:\uv\# 将目录添加到系统PATH环境变量# 验证安装uv --version
# 安装Scoop(如果没有)Set-ExecutionPolicy RemoteSigned -Scope CurrentUserirm get.scoop.sh |iex# 安装uvscoop install uv
# 安装Homebrew(如果没有)/bin/bash -c"$(curl-fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"# 安装uvbrew install uv# 或者使用brew tap brew tap astral-sh/uv brew install uv
# 一键安装curl -LsSf https://astral.sh/uv/install.sh |sh# 或指定版本curl -LsSf https://astral.sh/uv/0.4.3/install.sh |sh
# 使用系统Python或已有环境pip install uv# 或使用pipx(推荐用于工具类包)brew install pipx pipx install uv
# 下载最新版本curl-L https://github.com/astral-sh/uv/releases/latest/download/uv-aarch64-apple-darwin.tar.gz -o uv.tar.gz# 解压tar -xzf uv.tar.gz# 移动到可执行目录sudo mv uv /usr/local/bin/# 验证安装uv --version
# 查看当前配置uv config list# 设置镜像源(国内用户)uv config set registry-url "https://pypi.tuna.tsinghua.edu.cn/simple"uv config set index-url "https://pypi.tuna.tsinghua.edu.cn/simple"# 恢复默认源uv config unset registry-urluv config unset index-url
# 设置Python版本(自动下载)uv python install 3.11uv python install 3.12# 列出已安装的Python版本uv python list# 设置默认Python版本uv python pin 3.12
# 1. 创建新项目uv init myproject --packagecd myproject# 2. 添加依赖uv add fastapi uvicorn[standard]uv add pytest pytest-asyncio --dev# 3. 创建虚拟环境uv venv# 或指定Python版本uv venv --python 3.12# 4. 激活环境# Windows: .venv\Scripts\activate# macOS: source .venv/bin/activate# 5. 安装依赖uv sync# 6. 开发代码...# 7. 运行测试uv run pytest# 8. 打包发布uv build
# 创建虚拟环境uv venv .venv# 激活虚拟环境# Windows:.venv\Scripts\activate# macOS/Linux:source .venv/bin/activate# 使用虚拟环境执行命令(无需激活)uv run --with .venv python script.py# 删除虚拟环境rm -rf .venv # 或直接删除文件夹
# 添加依赖uv add requestsuv add fastapi ">=0.100.0"uv add pytest --dev # 开发依赖# 同步依赖(根据pyproject.toml更新环境)uv sync# 安装need_package依赖uv pip install need_package# 从requirements.txt安装uv pip install -r requirements.txt# 移除依赖uv remove requests# 列出已安装包uv pip list# 导出依赖uv pip freeze > requirements.txt
# 安装打包工具uv add pyinstaller --dev# 打包单文件uv run pyinstaller --onefile app.py# 打包带依赖的项目uv run pyinstaller --name MyApp --add-data "data:data" main.py# 跨平台打包# 需要对应平台的Python环境
# Dockerfile示例FROM python:3.12-slim# 安装uvRUN pip install uvWORKDIR /app# 复制依赖文件COPY pyproject.toml uv.lock ./# 安装依赖RUN uv sync --frozen# 复制源代码COPY . .# 运行应用CMD ["uv", "run", "python", "app.py"]
# 安装pexuv add pex --dev# 创建pex可执行文件uv run pex . -o myapp.pex -c main# 运行pex文件./myapp.pex
# 构建项目uv build# 将生成dist/目录包含:# - myproject-0.1.0-py3-none-any.whl# - myproject-0.1.0.tar.gz# 发布到PyPIuv publish# 或使用twineuv add twine --devuv run twine upload dist/*
# 导出精确环境uv pip freeze --exclude-editable > requirements.lock# 从锁文件恢复环境uv sync --requirements-file requirements.lock# 复制环境到另一台机器# 1. 导出环境uv pip freeze > requirements.txt# 2. 在新机器上uv sync --requirements-file requirements.txt
# 生成跨平台requirementsuv pip compile pyproject.toml \ --platform linux_x86_64 \ --platform macosx_12_0_arm64 \ --platform win_amd64 \ -o requirements-all.txt