python---使用pipreqs及遇到的问题

pipreqs简介

​ 项目开发的过程中, 避免不了搭建和部署开发环境, 而搭建和部署开发环境需要项目依赖的python第三方包, 如何获取一个项目中所需依赖的python第三方包, 这就需要使用pipreqs包, 它可以自动生成项目中依赖的第三方包, 并能生成requirements.txt文件, 方便在搭建和部署项目开发环境时安装依赖包.

pipreqs安装

pip install pipreqs

​ 可以使用--help参数, 查看pipreqs支持的参数

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

pipreqs与freeze的区别

​ pipreqs对项目目录进行扫描, 发现使用了哪些第三方包, 自动生成依赖包列表. 但有时候可能会有一点偏差, 需要检查调整一下.

​ freeze配合虚拟环境使用时效果更好, 因为freeze是把整个环境中的包都列出来, 不论项目中是否使用了, 都会被列出来.

pipreqs使用时遇到的问题

出现UnicodeDecodeError

​ 出现编码错误时, 可以指定编码格式

pipreqs ./ --encoding=utf-8

出现SyntaxError

​ 出现语法错误时, 一般时python2和python3之间的不兼容问题, 可以使用--debug找到问题文件, 然后--ignore忽略掉问题文件所在的目录.

pipreqs ./ --encoding=utf-8 --debug
pipreqs ./ --encoding=utf-8 --ignore dir目录
原文地址:https://www.cnblogs.com/KX-Lau/p/14302389.html