Skip to main content

Add your description here

Project description

uv文档

  1. https://docs.astral.sh/uv/reference/cli/#uv

新建一个uv项目

  1. uv init uv_dependencie_demo 项目初始化
  2. cd uv_dependencie_demo 进入项目目录
  3. uv add "fastapi" 添加依赖包,内容会出现在pyproject.toml的dependencies中
  4. pip install "fastapi" 安装依赖包
  5. 对于步骤3和4,如果使用pycharm,引入依赖时,直接通过pycharm添加,会安装和添加到pyproject.toml的dependencies中

已有uv项目安装依赖

  1. 怎么命令安装pyproject.toml的dependencies中依赖 uv sync
  2. 补充说明 虚拟环境要求:uv sync 会自动关联当前目录的虚拟环境(如 .venv),无需手动激活。 依赖解析:uv 会解析 pyproject.toml 的依赖树,并下载/安装所有依赖及其子依赖。 替代方案:若需类似 pip install -r requirements.txt 的行为,可使用 uv pip install -r requirements.txt,但直接使用 pyproject.toml 更符合现代 Python 项目规范。

uv项目启动

  1. uv run main.py

uv使用详细流程

使用 uv 管理 Python 项目的详细流程如下,结合示例说明关键步骤:


1. 初始化项目

在项目根目录运行以下命令:

uv init
  • 作用:生成 pyproject.toml.venv 虚拟环境目录。
  • 输出示例
    [project]
    name = "my-project"
    version = "0.1.0"
    dependencies = []
    

2. 添加依赖

使用 uv add 安装包并自动更新 pyproject.tomluv.lock

uv add fastapi uvicorn
  • 效果pyproject.tomldependencies 列表新增 fastapiuvicorn
  • 可选依赖:添加测试依赖时使用 --extra
    uv add --extra test pytest
    
    这会在 pyproject.toml 中生成:
    [project.optional-dependencies]
    test = ["pytest"]
    

3. 运行命令

通过 uv run 在隔离环境中执行脚本或命令:

uv run python main.py
  • 自动同步uv 会先验证 uv.lock 是否与 pyproject.toml 同步,并确保虚拟环境已安装所有依赖
  • 示例:运行 FastAPI 应用:
    uv run uvicorn main:app --reload
    

4. 管理虚拟环境

  • 手动激活环境(非必需):
    source .venv/bin/activate  # Linux/macOS
    .venv\Scripts\activate     # Windows
    
  • 强制同步依赖
    uv sync
    uv sync --extra test       # 安装可选依赖
    

5. 项目结构示例

最终目录结构如下:

my-project/
├── .venv/                # 虚拟环境
├── pyproject.toml        # 项目配置
├── uv.lock               # 依赖锁文件
├── main.py               # 主程序文件
└── tests/                # 测试目录
    └── test_main.py

6. 常见问题与解决方案

Q1: Windows 路径报错(ModuleNotFoundError)

  • 原因:脚本路径使用反斜杠 \ 导致模块导入失败
  • 解决:确保 sys.path 包含项目根目录,或使用正斜杠 /
    uv run python scripts/test.py
    

Q2: 依赖冲突

  • 现象:安装依赖时提示 no solution found
  • 解决:升级/降级依赖版本,或使用 --upgrade-package
    uv lock --upgrade-package numpy
    

7. 完整示例:创建并运行 FastAPI 项目

  1. 初始化项目
    uv init
    
  2. 添加依赖
    uv add fastapi uvicorn
    
  3. 编写 main.py
    from fastapi import FastAPI
    app = FastAPI()
    @app.get("/")
    def read_root():
        return {"Hello": "World"}
    
  4. 启动服务
    uv run uvicorn main:app --reload
    
  5. 访问:浏览器打开 http://127.0.0.1:8000

8. 高级用法:构建可分发包

  1. 配置 pyproject.toml 的构建系统
    [build-system]
    requires = ["setuptools"]
    build-backend = "setuptools.build_meta"
    
  2. 构建 wheel 包
    uv build --wheel
    

通过上述流程,可高效管理 Python 项目的依赖、环境和运行逻辑。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uv_dependencie_demo-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uv_dependencie_demo-0.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file uv_dependencie_demo-0.1.0.tar.gz.

File metadata

  • Download URL: uv_dependencie_demo-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for uv_dependencie_demo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9e26019f08bbaa0b5695c0b3d757185532f963de1d3cef937d1e97dc64b73748
MD5 3a6062b10cee72c267f7982ab3baf557
BLAKE2b-256 fa7955a3511e4814454b7a71ff0e4dcfbcb62a8d86e3db90ea6b9a83b392f755

See more details on using hashes here.

File details

Details for the file uv_dependencie_demo-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for uv_dependencie_demo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80af7f40d7fa60e7c77181494756a01c3b0e6f410b2b0eeab709fac86a0eaabc
MD5 2d8e1ea2ebf7e449f1ce72136a24e097
BLAKE2b-256 53db594cab7263410287864af4e95c028d163b80f588e07af153bcce9c93aa96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page