使用pipreqs生成requirements.txt

欢迎关注公众号【Python开发实战】,免费领取Python学习电子书!

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目录
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯旋.Lau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值