uv#
传说比conda好的python管理工具,正在学习,先来个完整的demo
demo#
# 1. 创建项目
uv init myapp
cd myapp
# 2. 指定Python版本
uv python pin 3.11
# 3. 添加依赖
uv add fastapi uvicorn
uv add --dev pytest httpx
# 4. 同步环境
uv sync
# 5. 运行应用
uv run uvicorn main:app --reload
# 6. 运行测试
uv run pytest安装#
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
# 或通过pip安装
pip install uv
uv --version #验证配置加速源#
# 临时指定清华源
uv add pandas --default-index https://pypi.tuna.tsinghua.edu.cn/simple
# 或在pyproject.toml中永久配置
[[index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = truepython 版本管理#
uv python install 3.12
uv python install 3.10
#为项目锁定版本
uv python pin 3.11使用步骤#
创建虚拟环境
# 创建虚拟环境
uv venv myenv
# 指定Python版本创建
uv venv --python /path/to/python myenv
#Linux/macOS下使用
source myenv/bin/activate
#Windows下使用
myenv\Scripts\activate使用:
# 创建项目
uv init myproject
# 指定Python版本创建
uv init myproject -p 3.11.10
# 添加单个依赖
uv add requests
# 一次添加多个
uv add pandas numpy
# 添加开发环境依赖
uv add --group dev pytest ruff
# 删除依赖
uv remove requests
# 查看依赖树
uv tree
# 首次进入项目自动创建虚拟环境并安装依赖
uv sync
# 锁定版本,严格按照版本下载
uv lock
uv sync --frozen运行脚本
# 在项目虚拟环境中运行
uv run hello.py
# 运行指定Python版本
uv run --python 3.10 main.py导出环境#
uv export --format requirements.txt > requirements.txt兼容pip的使用方法#
# 安装包到系统环境
uv pip install requests --system
# 从requirements.txt安装
uv pip install -r requirements.txt
# 导出当前环境依赖
uv pip freeze > requirements.txt
# 编译依赖
uv pip compile requirements.in -o requirements.txt