Python项目requirements.txt依赖包如何生成?

简介: Python项目requirements.txt依赖包如何生成?

1 目的

  • 将项目部署到其他的环境,快速安装当前项目所需要的依赖包;
  • 确保安装的依赖包和项目保持一致,避免出现版本错误引起环境异常。

2 所有依赖包

  • 如果想导出本地环境所有的Python包,即pip安装的所有包,直接使用:
pip freeze > requirements.txt
  • 如图:

在这里插入图片描述
在这里插入图片描述

3 项目依赖包

3.1 安装pipreqs

  • 以上显然不是我们常用的,因为我们一般使用的是把某个项目的依赖包导出来,便于项目在其他环境部署安装,所以我们需要的是导出项目的依赖包;
  • 如下是一个py文件,我们导入了一些包;

在这里插入图片描述

  • 那如果别人想要用这个脚本,他就需要安装这些包;
  • 那我们需要把这些包对应的版本导出来,需使用Python的pipreqs包;
  • pipreqs可以导出项目所需要的依赖包;
  • 安装 pipreqs
pip install pipreqs
  • 如图是我们已经安装好的:

在这里插入图片描述

3.2 导出项目依赖包

  • 直接使用:
pipreqs . --encoding=utf8 --force
  • 参数说明:
# “.” 指的是将导出依赖包的文件放在当前目录下

# “--encoding=utf8” 指的是存放文件的编码为utf-8,否则会报错

# “--force” --force 强制执行,当生成目录下的requirements.txt存在时强子覆盖
  • 如图是我们导出的包:

在这里插入图片描述

3.3 其他环境使用

  • 在新的环境下,进入项目根目录执行:
pip install -r requirements.txt

3.4 pipreqs参数说明

  • 直接查看帮助文档,说的很详细了~
C:\Users\Administrator>pipreqs -h
pipreqs - Generate pip requirements.txt file based on imports

Usage:
    pipreqs [options] [<path>]

Arguments:
    <path>                The path to the directory containing the application
                          files for which a requirements file should be
                          generated (defaults to the current working
                          directory).

Options:
    --use-local           Use ONLY local package info instead of querying PyPI.
    --pypi-server <url>   Use custom PyPi server.
    --proxy <url>         Use Proxy, parameter will be passed to requests
                          library. You can also just set the environments
                          parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --ignore <dirs>...    Ignore extra directories, each separated by a comma
    --no-follow-links     Do not follow symbolic links in the project
    --encoding <charset>  Use encoding parameter for file open
    --savepath <file>     Save the list of requirements in the given file
    --print               Output the list of requirements in the standard
                          output
    --force               Overwrite existing requirements.txt
    --diff <file>         Compare modules in requirements.txt to project
                          imports
    --clean <file>        Clean up requirements.txt by removing modules
                          that are not imported in project
    --mode <scheme>       Enables dynamic versioning with <compat>,
                          <gt> or <non-pin> schemes.
                          <compat> | e.g. Flask~=1.1.2
                          <gt>     | e.g. Flask>=1.1.2
                          <no-pin> | e.g. Flask
目录
相关文章
|
9月前
|
异构计算 Python
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
709 1
|
9月前
|
API 语音技术 开发者
Python 项目打包,并上传到 PyPI,分享项目
本文介绍了如何使用 Poetry 打包并发布一个 Python 项目至 PyPI。内容包括:项目创建、配置 `pyproject.toml` 文件、构建软件包、上传至 PyPI、安装与使用。通过实例 iGTTS 展示了从开发到发布的完整流程,帮助开发者快速分享自己的 Python 工具。
|
10月前
|
数据处理 开发工具 开发者
requirement.txt 管理python包依赖
在 Python 项目中,`requirements.txt` 用于记录依赖库及其版本,便于环境复现。本文介绍了多种生成该文件的方法:基础方法使用 `pip freeze`,进阶方法使用 `pipreqs`,专业方法使用 `poetry` 或 `pipenv`,以及手动维护方式。每种方法适用不同场景,涵盖从简单导出到复杂依赖管理,并提供常见问题的解决方案,帮助开发者高效生成精准的依赖列表,确保项目环境一致性。
2909 4
|
9月前
|
人工智能 Shell Python
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
335 0
|
9月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1426 102
|
9月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
480 104
|
9月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
371 103
|
9月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
394 82
|
8月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
488 3
|
8月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
715 3

推荐镜像

更多