文章目录
- # 基本格式:包名==版本号 numpy==1.21.0 pandas==1.3.3 requests==2.26.0 # 注释以 # 开头 # 这是一个注释 # 空行会被忽略 matplotlib==3.4.3
- # 1. 精确版本 numpy==1.21.0 # 2. 最小版本 numpy>=1.21.0 # 3. 兼容版本(推荐) numpy~=1.21.0 # 等同于 >=1.21.0, <1.22.0 # 4. 版本范围 numpy>=1.20.0,<1.22.0 # 5. 不等于某版本 numpy!=1.21.1 # 6. 最新版本(不推荐用于生产环境) numpy
- # 从 Git 仓库安装 git+https://github.com/user/repo.git@v1.0.0 # 从本地路径安装 -e ./local-package # 包含额外依赖 requests[security]==2.26.0 # 从其他 requirements 文件引用 -r base-requirements.txt # 指定索引 URL –index-url https://pypi.org/simple/ –extra-index-url https://pypi.tuna.tsinghua.edu.cn/simple/
- 在项目根目录创建 requirements.txt 文件 手动添加依赖包和版本号 # Web 框架 django==4.2.0 flask==2.3.0 # 数据处理 numpy==1.24.0 pandas==2.0.0 # 数据库 sqlalchemy==2.0.0 psycopg2-binary==2.9.0 # 工具库 requests==2.28.0 python-dotenv==1.0.0
- # 安装包时同时记录到 requirements.txt echo “numpy==1.24.0” >> requirements.txt echo “pandas==2.0.0” >> requirements.txt
- # 生成当前环境所有包的依赖列表 pip freeze > requirements.txt # 查看生成的文件内容 cat requirements.txt 优点: 简单快捷 包含所有已安装的包 版本号精确 缺点: 包含不必要的依赖 可能包含系统级包
- # 安装 pipreqs pip install pipreqs # 在项目目录中生成 requirements.txt pipreqs . –encoding=utf8 # 强制覆盖已存在的文件 pipreqs . –force –encoding=utf8 # 指定输出文件名 pipreqs . –savepath=my-requirements.txt 优点: 只包含项目实际使用的包 自动分析代码中的 import 语句 生成的文件更简洁 缺点: 需要额外安装工具 可能遗漏动态导入的包
- # 安装 pip-tools pip install pip-tools # 创建 requirements.in 文件(高级依赖) echo “django” > requirements.in echo “requests” >> requirements.in # 生成详细的 requirements.txt pip-compile requirements.in # 更新依赖 pip-compile –upgrade requirements.in
- # 安装 Poetry curl -sSL https://install.python-poetry.org | python3 – # 初始化项目 poetry init # 添加依赖 poetry add django requests # 导出 requirements.txt poetry export -f requirements.txt –output requirements.txt
- # 生产环境:使用精确版本 django==4.2.1 numpy==1.24.3 # 开发环境:可以使用兼容版本 django~=4.2.0 numpy~=1.24.0
- # 项目结构 requirements/ ├── base.txt # 基础依赖 ├── development.txt # 开发依赖 ├── production.txt # 生产依赖 └── testing.txt # 测试依赖 base.txt: django==4.2.1 requests==2.28.2 sqlalchemy==2.0.10 development.txt: -r base.txt # 开发工具 django-debug-toolbar==4.0.0 ipython==8.12.0 black==23.3.0 flake8==6.0.0 production.txt: -r base.txt # 生产环境专用 gunicorn==20.1.0 psycopg2-binary==2.9.6 redis==4.5.4
- # 创建虚拟环境 python -m venv myenv # 激活虚拟环境 # Windows myenvScriptsactivate # Linux/Mac source myenv/bin/activate # 安装依赖 pip install -r requirements.txt # 生成新的 requirements.txt pip freeze > requirements.txt
- # 错误信息示例 ERROR: pip’s dependency resolver does not currently consider all the ways your dependencies can conflict with each other. 解决方案: # 使用 pip-tools 解决冲突 pip install pip-tools pip-compile –resolver=backtracking requirements.in # 或者手动调整版本 # 将冲突的包版本调整为兼容版本
- # 常见错误 ERROR: Could not find a version that satisfies the requirement 解决方案: # 1. 更新 pip pip install –upgrade pip # 2. 使用国内镜像源 pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ # 3. 检查 Python 版本兼容性 python –version # 4. 清理缓存 pip cache purge
- # 更新所有包到最新版本 pip list –outdated pip install –upgrade -r requirements.txt # 或使用 pip-review pip install pip-review pip-review –local –interactive
- # 使用 pipdeptree 查看依赖树 pip install pipdeptree pipdeptree # 只保留顶级依赖 pipreqs . –force
- # 根据 Python 版本安装不同的包 typing-extensions>=3.7.4; python_version<“3.8″ # 根据操作系统安装 pywin32>=227; sys_platform==”win32″ # 根据平台架构 tensorflow-macos>=2.9.0; platform_machine==”arm64″ and platform_system==”Darwin” tensorflow>=2.9.0; platform_machine!=”arm64″ or platform_system!=”Darwin”
- # 主要功能 requests==2.28.2 # 可选功能(需要手动安装) # pip install requests[security] # pip install requests[socks]
- # 代码格式化 black==23.3.0 isort==5.12.0 # 代码检查 flake8==6.0.0 mypy==1.2.0 # 测试工具 pytest==7.3.0 pytest-cov==4.0.0 # 文档生成 sphinx==6.1.3
- # 安装安全扫描工具 pip install safety # 扫描 requirements.txt 中的安全漏洞 safety check -r requirements.txt # 生成安全报告 safety check -r requirements.txt –json > security-report.json
- # Django Web 项目 requirements.txt # Web 框架 Django==4.2.1 django-cors-headers==4.0.0 django-rest-framework==3.14.0 # 数据库 psycopg2-binary==2.9.6 django-redis==5.2.0 # 认证和安全 django-allauth==0.54.0 cryptography==40.0.2 # 文件处理 Pillow==9.5.0 django-storages==1.13.2 boto3==1.26.137 # 任务队列 celery==5.2.7 redis==4.5.4 # 监控和日志 sentry-sdk==1.22.2 django-extensions==3.2.1 # 开发工具(仅开发环境) # django-debug-toolbar==4.0.0 # ipython==8.12.0
- # 数据科学项目 requirements.txt # 核心数据处理 numpy==1.24.3 pandas==2.0.1 scipy==1.10.1 # 机器学习 scikit-learn==1.2.2 xgboost==1.7.5 lightgbm==3.3.5 # 深度学习 tensorflow==2.12.0 torch==2.0.0 torchvision==0.15.0 # 数据可视化 matplotlib==3.7.1 seaborn==0.12.2 plotly==5.14.1 # Jupyter 环境 jupyter==1.0.0 ipykernel==6.22.0 nbconvert==7.3.1 # 数据库连接 sqlalchemy==2.0.10 pymongo==4.3.3 # 工具库 requests==2.28.2 tqdm==4.65.0 python-dotenv==1.0.0
- # FastAPI 服务项目 requirements.txt # Web 框架 fastapi==0.95.2 uvicorn[standard]==0.22.0 # 数据验证 pydantic==1.10.7 email-validator==2.0.0 # 数据库 sqlalchemy==2.0.10 alembic==1.10.4 psycopg2-binary==2.9.6 # 认证 python-jose[cryptography]==3.3.0 passlib[bcrypt]==1.7.4 python-multipart==0.0.6 # HTTP 客户端 httpx==0.24.1 aiofiles==23.1.0 # 缓存 redis==4.5.4 aioredis==2.0.1 # 配置管理 python-dotenv==1.0.0 pydantic-settings==2.0.0 # 监控 prometheus-client==0.16.0 # 测试(开发环境) # pytest==7.3.1 # pytest-asyncio==0.21.0 # httpx==0.24.1
- # 自动化脚本项目 requirements.txt # Web 自动化 selenium==4.9.0 beautifulsoup4==4.12.2 requests==2.28.2 # 文件处理 openpyxl==3.1.2 Pillow==9.5.0 PyPDF2==3.0.1 # 系统操作 psutil==5.9.5 watchdog==3.0.0 schedule==1.2.0 # 配置和日志 python-dotenv==1.0.0 loguru==0.7.0 click==8.1.3 # 网络工具 requests==2.28.2 urllib3==2.0.2 # 数据处理 pandas==2.0.1 numpy==1.24.3
目录
- 什么是 requirements.txt
- requirements.txt 的作用
- 🎯 主要功能
- 💡 使用场景
- 文件格式说明
- 基本格式
- 版本指定方式
- 高级格式
- 手动创建 requirements.txt
- 方法一:直接编辑
- 方法二:逐步添加
- 自动生成 requirements.txt
- 方法一:使用 pip freeze(最常用)
- 方法二:使用 pipreqs(推荐)
- 方法三:使用 pip-tools
- 方法四:使用 Poetry(现代化方案)
- 版本控制最佳实践
- 1. 版本固定策略
- 2. 分层依赖管理
- 3. 虚拟环境配合使用
- 常见问题与解决方案
- 问题1:依赖冲突
- 问题2:安装失败
- 问题3:包版本过旧
- 问题4:requirements.txt 过大
- 高级用法
- 1. 条件依赖
- 2. 可选依赖
- 3. 开发工具集成
- 4. 安全扫描
- 实际应用示例
- 示例1:Django Web 项目
- 示例2:数据科学项目
- 示例3:API 服务项目
- 示例4:自动化脚本项目
- 📚 总结
- 最佳实践建议
requirements.txt 是 Python 项目中用于记录项目依赖包及其版本信息的文本文件。它是 Python 生态系统中管理项目依赖的标准方式,类似于 Node.js 的 package.json 或 Java 的 pom.xml。
- 依赖管理:记录项目所需的所有第三方库
- 环境复现:确保不同环境中的依赖版本一致
- 团队协作:团队成员可以快速搭建相同的开发环境
- 部署简化:生产环境可以一键安装所有依赖
- 版本控制:跟踪依赖包的版本变化
- 项目开发和维护
- 代码版本控制
- 持续集成/持续部署 (CI/CD)
- Docker 容器化部署
- 虚拟环境管理
# 基本格式:包名==版本号
numpy==1.21.0
pandas==1.3.3
requests==2.26.0
# 注释以 # 开头
# 这是一个注释
# 空行会被忽略
matplotlib==3.4.3
# 1. 精确版本
numpy==1.21.0
# 2. 最小版本
numpy>=1.21.0
# 3. 兼容版本(推荐)
numpy~=1.21.0 # 等同于 >=1.21.0, <1.22.0
# 4. 版本范围
numpy>=1.20.0,<1.22.0
# 5. 不等于某版本
numpy!=1.21.1
# 6. 最新版本(不推荐用于生产环境)
numpy
# 从 Git 仓库安装
git+https://github.com/user/repo.git@v1.0.0
# 从本地路径安装
-e ./local-package
# 包含额外依赖
requests[security]==2.26.0
# 从其他 requirements 文件引用
-r base-requirements.txt
# 指定索引 URL
--index-url https://pypi.org/simple/
--extra-index-url https://pypi.tuna.tsinghua.edu.cn/simple/
- 在项目根目录创建
requirements.txt 文件
- 手动添加依赖包和版本号
# Web 框架
django==4.2.0
flask==2.3.0
# 数据处理
numpy==1.24.0
pandas==2.0.0
# 数据库
sqlalchemy==2.0.0
psycopg2-binary==2.9.0
# 工具库
requests==2.28.0
python-dotenv==1.0.0
requirements.txt 文件
# 安装包时同时记录到 requirements.txt
echo "numpy==1.24.0" >> requirements.txt
echo "pandas==2.0.0" >> requirements.txt
# 生成当前环境所有包的依赖列表
pip freeze > requirements.txt
# 查看生成的文件内容
cat requirements.txt
优点:
- 简单快捷
- 包含所有已安装的包
- 版本号精确
缺点:
- 包含不必要的依赖
- 可能包含系统级包
# 安装 pipreqs
pip install pipreqs
# 在项目目录中生成 requirements.txt
pipreqs . --encoding=utf8
# 强制覆盖已存在的文件
pipreqs . --force --encoding=utf8
# 指定输出文件名
pipreqs . --savepath=my-requirements.txt
优点:
- 只包含项目实际使用的包
- 自动分析代码中的 import 语句
- 生成的文件更简洁
缺点:
- 需要额外安装工具
- 可能遗漏动态导入的包
# 安装 pip-tools
pip install pip-tools
# 创建 requirements.in 文件(高级依赖)
echo "django" > requirements.in
echo "requests" >> requirements.in
# 生成详细的 requirements.txt
pip-compile requirements.in
# 更新依赖
pip-compile --upgrade requirements.in
# 安装 Poetry
curl -sSL https://install.python-poetry.org | python3 -
# 初始化项目
poetry init
# 添加依赖
poetry add django requests
# 导出 requirements.txt
poetry export -f requirements.txt --output requirements.txt
# 生产环境:使用精确版本
django==4.2.1
numpy==1.24.3
# 开发环境:可以使用兼容版本
django~=4.2.0
numpy~=1.24.0
# 项目结构
requirements/
├── base.txt # 基础依赖
├── development.txt # 开发依赖
├── production.txt # 生产依赖
└── testing.txt # 测试依赖
base.txt:
django==4.2.1 requests==2.28.2 sqlalchemy==2.0.10
development.txt:
-r base.txt # 开发工具 django-debug-toolbar==4.0.0 ipython==8.12.0 black==23.3.0 flake8==6.0.0
production.txt:
-r base.txt # 生产环境专用 gunicorn==20.1.0 psycopg2-binary==2.9.6 redis==4.5.4
# 创建虚拟环境
python -m venv myenv
# 激活虚拟环境
# Windows
myenvScriptsactivate
# Linux/Mac
source myenv/bin/activate
# 安装依赖
pip install -r requirements.txt
# 生成新的 requirements.txt
pip freeze > requirements.txt
# 错误信息示例
ERROR: pip's dependency resolver does not currently consider all the ways your dependencies can conflict with each other.
解决方案:
# 使用 pip-tools 解决冲突 pip install pip-tools pip-compile --resolver=backtracking requirements.in # 或者手动调整版本 # 将冲突的包版本调整为兼容版本
# 常见错误
ERROR: Could not find a version that satisfies the requirement
解决方案:
# 1. 更新 pip pip install --upgrade pip # 2. 使用国内镜像源 pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ # 3. 检查 Python 版本兼容性 python --version # 4. 清理缓存 pip cache purge
# 更新所有包到最新版本
pip list --outdated
pip install --upgrade -r requirements.txt
# 或使用 pip-review
pip install pip-review
pip-review --local --interactive
# 使用 pipdeptree 查看依赖树
pip install pipdeptree
pipdeptree
# 只保留顶级依赖
pipreqs . --force
# 根据 Python 版本安装不同的包
typing-extensions>=3.7.4; python_version<"3.8"
# 根据操作系统安装
pywin32>=227; sys_platform=="win32"
# 根据平台架构
tensorflow-macos>=2.9.0; platform_machine=="arm64" and platform_system=="Darwin"
tensorflow>=2.9.0; platform_machine!="arm64" or platform_system!="Darwin"
# 主要功能
requests==2.28.2
# 可选功能(需要手动安装)
# pip install requests[security]
# pip install requests[socks]
# 代码格式化
black==23.3.0
isort==5.12.0
# 代码检查
flake8==6.0.0
mypy==1.2.0
# 测试工具
pytest==7.3.0
pytest-cov==4.0.0
# 文档生成
sphinx==6.1.3
# 安装安全扫描工具
pip install safety
# 扫描 requirements.txt 中的安全漏洞
safety check -r requirements.txt
# 生成安全报告
safety check -r requirements.txt --json > security-report.json
# Django Web 项目 requirements.txt
# Web 框架
Django==4.2.1
django-cors-headers==4.0.0
django-rest-framework==3.14.0
# 数据库
psycopg2-binary==2.9.6
django-redis==5.2.0
# 认证和安全
django-allauth==0.54.0
cryptography==40.0.2
# 文件处理
Pillow==9.5.0
django-storages==1.13.2
boto3==1.26.137
# 任务队列
celery==5.2.7
redis==4.5.4
# 监控和日志
sentry-sdk==1.22.2
django-extensions==3.2.1
# 开发工具(仅开发环境)
# django-debug-toolbar==4.0.0
# ipython==8.12.0
# 数据科学项目 requirements.txt
# 核心数据处理
numpy==1.24.3
pandas==2.0.1
scipy==1.10.1
# 机器学习
scikit-learn==1.2.2
xgboost==1.7.5
lightgbm==3.3.5
# 深度学习
tensorflow==2.12.0
torch==2.0.0
torchvision==0.15.0
# 数据可视化
matplotlib==3.7.1
seaborn==0.12.2
plotly==5.14.1
# Jupyter 环境
jupyter==1.0.0
ipykernel==6.22.0
nbconvert==7.3.1
# 数据库连接
sqlalchemy==2.0.10
pymongo==4.3.3
# 工具库
requests==2.28.2
tqdm==4.65.0
python-dotenv==1.0.0
# FastAPI 服务项目 requirements.txt
# Web 框架
fastapi==0.95.2
uvicorn[standard]==0.22.0
# 数据验证
pydantic==1.10.7
email-validator==2.0.0
# 数据库
sqlalchemy==2.0.10
alembic==1.10.4
psycopg2-binary==2.9.6
# 认证
python-jose[cryptography]==3.3.0
passlib[bcrypt]==1.7.4
python-multipart==0.0.6
# HTTP 客户端
httpx==0.24.1
aiofiles==23.1.0
# 缓存
redis==4.5.4
aioredis==2.0.1
# 配置管理
python-dotenv==1.0.0
pydantic-settings==2.0.0
# 监控
prometheus-client==0.16.0
# 测试(开发环境)
# pytest==7.3.1
# pytest-asyncio==0.21.0
# httpx==0.24.1
# 自动化脚本项目 requirements.txt
# Web 自动化
selenium==4.9.0
beautifulsoup4==4.12.2
requests==2.28.2
# 文件处理
openpyxl==3.1.2
Pillow==9.5.0
PyPDF2==3.0.1
# 系统操作
psutil==5.9.5
watchdog==3.0.0
schedule==1.2.0
# 配置和日志
python-dotenv==1.0.0
loguru==0.7.0
click==8.1.3
# 网络工具
requests==2.28.2
urllib3==2.0.2
# 数据处理
pandas==2.0.1
numpy==1.24.3
requirements.txt 是 Python 项目依赖管理的核心工具,正确使用它可以:
- 提高开发效率:快速搭建开发环境
- 确保环境一致性:避免"在我机器上能运行"的问题
- 简化部署流程:一键安装所有依赖
- 便于团队协作:统一的依赖管理标准
- 支持版本控制:跟踪依赖变化历史
- ✅ 使用虚拟环境隔离项目依赖
- ✅ 定期更新和维护 requirements.txt
- ✅ 使用版本固定避免意外更新
- ✅ 分层管理不同环境的依赖
- ✅ 定期进行安全扫描
- ❌ 避免在全局环境中生成 requirements.txt
- ❌ 不要忽略依赖冲突警告
- ❌ 避免使用过于宽泛的版本范围
通过合理使用 requirements.txt,你的 Python 项目将更加规范、可维护和可部署。
到此这篇关于Python requirements.txt使用小结的文章就介绍到这了,更多相关Python requirements.txt使用内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- python生成requirements.txt的两种方法
- 详解python中requirements.txt的一切
- python 中的requirements.txt 文件的使用详情
- python导出requirements.txt的几种方法总结
- python生成requirements.txt文件的两种方法
- Python pip通过requirements.txt 文件安装依赖
- Python如何生成requirements.txt
- python项目下生成requirements.txt方法
- Python requirements.txt的具体使用
- python生成requirements.txt文件的推荐方法
- python项目中requirements.txt的用法实例教程