Python之pipreqs自动生成requirements.txt

0 背景

当我们要把我们的 python 项目发布,或打包给别人使用时,需要指定各个软件的版本,即生成 requirements.txt,这个过程可以自动生成,不需要手写。

有人会说,直接使用 pip freeze 工具也可以生成 requirements.txt,但这种方式有以下三个局限:

  1. pip freeze 只保存通过 pip install 安装的软件包;
  2. pip freeze 会保存当前用户下所有的 python 软件包,实际上有很多软件是其它项目中应用的(除非是在 virtualenv 虚拟环境中)
  3. 而我们的需求是只导出我们当前项目的软件环境

1 安装使用

项目地址:https://github.com/bndr/pipreqs

安装方法

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 /home/project/location
Successfully saved requirements file in /home/project/location/requirements.txt

requirements.txt 内容如下,可以看到清爽很多

wheel==0.23.0
Yarg==0.1.9
docopt==0.6.2

如果要更新 requirement,只需要强制重新生成即可

$ pipreqs --force /home/project/location

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宗而研之

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

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

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

打赏作者

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

抵扣说明:

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

余额充值