0 背景
当我们要把我们的 python 项目发布,或打包给别人使用时,需要指定各个软件的版本,即生成 requirements.txt,这个过程可以自动生成,不需要手写。
有人会说,直接使用 pip freeze 工具也可以生成 requirements.txt,但这种方式有以下三个局限:
- pip freeze 只保存通过 pip install 安装的软件包;
- pip freeze 会保存当前用户下所有的 python 软件包,实际上有很多软件是其它项目中应用的(除非是在 virtualenv 虚拟环境中)
- 而我们的需求是只导出我们当前项目的软件环境
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