一、项目初始化
创建新项目,生成 pyproject.toml和 .venv虚拟环境
uv init
指定 Python 版本初始化项目
uv init myapp --python 3.12
二、虚拟环境管理
激活
.venv\Scripts\activate
#创建或更新虚拟环境(默认路径 .venv)
uv venv
#指定 Python 版本创建虚拟环境
uv venv --python 3.12
三、依赖管理
uv add requests==2.31.0 # 添加指定版本依赖,自动更新 pyproject.toml和 uv.lock
uv add -r requirements.txt #从文件批量添加依赖
uv remove requests #移除依赖并更新锁文件
uv sync #根据锁文件同步虚拟环境,确保依赖一致性
四、运行与调试
uv run main.py #在虚拟环境中运行脚本
uv run --python 3.12 app.py #指定 Python 版本运行脚本
五、Python 版本管理
uv python list #列出已安装的 Python 版本
uv python install 3.12 #安装指定 Python 版本