pipreqs

  • Post author:
  • Post category:其他

一、说明

使用 pipreqs 可以自动检索到当前项目下的所有组件及其版本,并生成 requirements.txt 文件,极大方便了项目迁移和部署的包管理。相比直接用 requirements.txt 命令,能直接隔离其它项目的包生成。

二、安装

pip install pipreqs

三、官方例子

  • 帮助文档
Usage:
    pipreqs [options] <path>

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
    --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.
    --no-pin              Omit version of output packages.

执行 pipreqs 命令

# Successfully saved requirements file in /home/project/location/requirements.txt
$ pipreqs /home/project/location

四、项目执行

  • 当前项目根目录下,执行 pipreqs 命令,不加 –encoding=utf8 会出现编码问题
> pipreqs ./ --encoding=utf-8
> pipreqs . --encoding=utf-8
  • 强制执行命令 –force ,覆盖原有的 requirements.txt 文件
> pipreqs ./ --encoding=utf-8 --force

五、参考文档

https://pypi.org/project/pipreqs/