# python-study **Repository Path**: wangdongping/python-study ## Basic Information - **Project Name**: python-study - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-05 - **Last Updated**: 2025-09-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 项目(依赖)管理 ## 第 1 种方案 :手动处理 1. 项目开发阶段,使用 pip install 安装依赖 2. 项目完成时 ,使用 pip freeze 导出依赖到文件 ```shell pip freeze > requirements.txt ``` 3. 导入别人项目时,使用 pip install -r requirements.txt 安装依赖 ```shell pip install -r requirements.txt ``` ## 第 2 种方案:使用 pyproject.toml 管理项目依赖 pyproject.toml 是项目的配置文件,用于描述项目的元数据,如项目名称、版本、依赖项、测试命令等。 1. 在项目根目录 创建 pyproject.toml 文件,并添加以下内容: ```toml [project] name = "myproject" version = "0.1.0" dependencies = [ 'fastapi==0.115.12', 'uvicorn==0.34.3', 'sqlmodel==0.0.24', ] ``` 2. 导入别人项目时,在根目录使用 “pip install -e . ”命令安装项目时,会自动安装依赖项 ```shell pip install -e . ``` [缺点] : 1. 需要手动添加依赖项 ## 第 3 种方案:使用 pyproject.toml + uv 命令 * poetry 是一个 Python 包管理工具,用于管理 Python 项目的依赖项。 * uv 命令是一个新型的 超高速 Python 包管理器和构建工具。 * pdm 是一个 Python 包管理工具,用于管理 Python 项目的依赖项。 **这里使用 uv 命令 : pip install uv** 1. 创建项目 ```shell uv init myproject ``` 2. 虚拟环境 ```shell # 手动指定虚拟环境路径,默认是当前目录下的.venv # 默认的虚拟环境 在执行 uv add 命令时自动创建 uv venv D:\service\python\virtualenv\fast-api --python 3.9 # 激活该虚拟环境 D:\service\python\virtualenv\fast-api\Scripts\activate.bat ``` 3. 安装依赖 ```shell uv add --active fastapi uvicorn sqlmodel ``` “uv add“ 命令会自动执行的操作: * 创建虚拟环境; * 创建pyproject.toml文件; * 自动安装依赖。 4. 移除依赖 ```shell uv remove fastapi ``` 5. 导入别人项目时,使用 uv sync 命令安装项目时,会自动安装依赖项 ```shell uv sync # 或者 uv pip install -e . ``` 6. 清除缓存 ```shell uv cache clean ``` 7. 指定安装源安装 ```shell uv add --index https://pypi.org/simple fastapi uv add --default-index https://pypi.org/simple fastapi ```