python 如何将项目目录所有.py文件的import导包汇总到requirements.txt文件 (pipreqs)(失败了)

编程入门 行业动态 更新时间:2024-10-22 16:38:05

python 如何将项目目录所有.py<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件的import导包汇总到requirements.txt文件 (pipreqs)(失败了)"/>

python 如何将项目目录所有.py文件的import导包汇总到requirements.txt文件 (pipreqs)(失败了)

文章目录

      • 作用:
      • 原理(20230809)
      • 使用方法:
      • 常见问题:
      • 以后要迁移项目, 要在新的虚拟环境安装包时, 只需在requirements.txt文件路径下执行`pip install -r ./requirements.txt`即可快速安装所有包.
    • 20230809 linux下测试
      • pipreqs --help
      • 实战(失败了)
    • 参考文章

作用:

能快速递归汇总目录下.py文件所需安装的包

原理(20230809)

pipreqs是一个Python库和命令行工具,用于自动分析Python项目的源代码,并生成一个包含项目所需依赖库的清单文件(通常是requirements.txt)。

pipreqs的工作原理如下:

  1. 遍历指定目录下的所有Python源代码文件,包括子目录中的文件。

  2. 分析源代码文件,识别出导入的外部库和模块。

  3. 根据导入的库和模块,确定项目所需的依赖库。

  4. 生成一个包含所有依赖库及其版本信息的清单文件。

在分析源代码文件时,pipreqs使用了一些启发式算法和规则来确定哪些导入语句是项目所需的依赖库。它会检查导入的库是否在Python标准库中,以及是否在项目的虚拟环境或全局环境中已经安装。

需要注意的是,pipreqs只能分析源代码文件中的导入语句,并不能检测运行时动态导入的库。因此,在使用pipreqs生成的依赖库清单文件后,还需要手动检查和添加可能的运行时依赖库。

总的来说,pipreqs是一个方便的工具,可以帮助你自动收集和管理Python项目的依赖库,减少手动维护requirements.txt文件的工作量。

使用方法:

  1. 执行pip install pipreqs
  2. 执行pipreqs ‘path’
    (会把项目path路径下所有.py文件以及子文件夹下的.py文件的导包汇总到一个txt文件)
    比如:

    然后我在当前路径打开控制台程序:

    然后执行:
    ('./'表示当前路径)

    没一会儿, 就会显示:

    文件夹中多了个requirements.txt文件:

    打开可看到我们的导包和包版本:

常见问题:

如果.py文件中全文绝对不能出现中文逗号: “,”, 如:

或者:

否则会报错(UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x8e in position 311: illegal multibyte sequence):

尤其要注意, 若有报错, 一定是出现了中文逗号或者其他中文标点了(猜测但没试过)!

以后要迁移项目, 要在新的虚拟环境安装包时, 只需在requirements.txt文件路径下执行pip install -r ./requirements.txt即可快速安装所有包.

20230809 linux下测试

pipreqs --help

pipreqs - Generate pip requirements.txt file based on importsUsage:pipreqs [options] [<path>]Arguments:<path>                The path to the directory containing the applicationfiles for which a requirements file should begenerated (defaults to the current workingdirectory).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 requestslibrary. You can also just set the environmentsparameter in your terminal:$ export HTTP_PROXY=":3128"$ export HTTPS_PROXY=":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 standardoutput--force               Overwrite existing requirements.txt--diff <file>         Compare modules in requirements.txt to projectimports--clean <file>        Clean up requirements.txt by removing modulesthat 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
pipreqs - 基于imports生成pip requirements.txt文件用法:pipreqs [选项] [<路径>]参数:<路径>                包含应用程序文件的目录路径,用于生成requirements文件(默认为当前工作目录)。选项:--use-local           仅使用本地包信息,而不查询PyPI。--pypi-server <url>   使用自定义的PyPi服务器。--proxy <url>         使用代理,参数将传递给requests库。你也可以在终端中设置环境变量:$ export HTTP_PROXY=":3128"$ export HTTPS_PROXY=":1080"--debug               打印调试信息。--ignore <dirs>...    忽略额外的目录,每个目录用逗号分隔。--no-follow-links     不跟随项目中的符号链接。--encoding <charset>  使用指定的字符编码打开文件。--savepath <file>     将依赖列表保存到指定的文件中。--print               在标准输出中输出依赖列表。--force               覆盖现有的requirements.txt文件。--diff <file>         将requirements.txt中的模块与项目中的导入进行比较。--clean <file>        清理requirements.txt文件,删除项目中未导入的模块。--mode <scheme>       启用动态版本控制,使用<compat>、<gt>或<non-pin>方案。<compat> | 例如 Flask~=1.1.2<gt>     | 例如 Flask>=1.1.2<no-pin> | 例如 Flask

实战(失败了)

进入项目根目录:

进入虚拟环境:

source .venv-python3.8/bin/activate

安装pipreqs:

pip install -i  pipreqs

执行:pipreqs . --ignore .venv-python3.8

一开始卡半天,后来又报错了啥情况??

换个命令,不查询pypi:

pipreqs . --ignore .venv-python3.8 --use-local 

咋给我输出空文件呢???

换个命令,不忽略虚拟环境目录:

pipreqs . --use-local

咋回事,我的ip_change.py明明导入到是这些库:

莫名奇妙啊!!!

参考文章

参考文章: pipreqs - Generate requirements.txt file for any project based on imports

更多推荐

python 如何将项目目录所有.py文件的import导包汇总到requirements.txt文件 (pipreqs)(失败了)

本文发布于:2024-03-13 10:21:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1733767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   如何将   目录   项目   失败了

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!