pipreqs导出当前项目的所有组件及其版本
pipreqs介绍
最近写完的自动化脚本,分享给同事的时候发现依赖包很难解决(使用的不是virtualenv环境)。想起来之前看开源接口平台项目的时候可以一键下载依赖包,于是就找到了第三方包pipreqs,可以自动帮助我们自动生成requirements.txt
- github原文介绍:pipreqs-根据任何项目的导入生成pip requirements.txt文件
- 链接:https://github.com/bndr/pipreqs
这是个很受欢迎的用于管理项目中依赖库的工具,可以用“pip install pipreqs”命令来安装。它的主要特点有:
- 搜索依赖库的范围是基于目录的方式,很有针对性
- 搜索的依据是脚本中所 import 的内容
- 可以在未安装依赖库的环境上生成依赖文件
- 查找软件包信息时,可以指定查询方式(只在本地查询、在 PyPi 查询、或者在自定义的 PyPi 服务)
第一步:
下载pipreqs工具包(pip install pipreqs)
Microsoft Windows [版本 10.0.17134.1069] (c) 2018 Microsoft Corporation。保留所有权利。 C:\Users\wy.DESKTOP-KENPKKP\Desktop\Dingda\dingAPI>pip install pipreqs Collecting pipreqs Downloading https://files.pythonhosted.org/packages/f8/8d/2e7c15bc5fcab54f9c5b404b5668fdac65f5e3224b2116097fae1299fc98/pipreqs -0.4.9-py2.py3-none-any.whl Collecting docopt Downloading https://files.pythonhosted.org/packages/a2/55/8f8cab2afd404cf578136ef2cc5dfb50baa1761b68c9da1fb1e4eed343c9/docopt- 0.6.2.tar.gz Collecting yarg Downloading https://files.pythonhosted.org/packages/8b/90/89a2ff242ccab6a24fbab18dbbabc67c51a6f0ed01f9a0f41689dc177419/yarg-0. 1.9-py2.py3-none-any.whl Installing collected packages: docopt, yarg, pipreqs Running setup.py install for docopt ... done Successfully installed docopt-0.6.2 pipreqs-0.4.9 yarg-0.1.9
第二步:
切换到项目路径下,生成依赖包的txt文档(pipreqs ./ --encoding=utf8)
期间遇到报错:编码错误(UnicodeDecodeError: 'gbk' codec can't decode byte 0x95 in position 40)
C:\Users\wy.DESKTOP-KENPKKP\Desktop\Dingda\dingAPI>pipreqs ./ Traceback (most recent call last): File "c:\users\wy.desktop-kenpkkp\appdata\local\programs\python\python36\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) ...... extra_ignore_dirs=extra_ignore_dirs) File "c:\users\wy.desktop-kenpkkp\appdata\local\programs\python\python36\lib\site-packages\pipreqs\pipreqs.py", line 75, in ge t_all_imports contents = f.read() UnicodeDecodeError: 'gbk' codec can't decode byte 0x95 in position 40: illegal multibyte sequence
找到原因是因为在Windows下需要制定编码方式为:uft-8,操作如下
C:\Users\wy.DESKTOP-KENPKKP\Desktop\Dingda\dingAPI>pipreqs ./ --encoding=utf8 INFO: Successfully saved requirements file in ./requirements.txt
第三步:
查看需要的依赖包(type requirements.txt)
C:\Users\wy.DESKTOP-KENPKKP\Desktop\Dingda\dingAPI>type requirements.txt requests==2.18.4 xlutils==2.0.0 xmltodict==0.12.0 xlrd==1.2.0 dicttoxml==1.7.4
下载依赖包方法(pip install -r requriements.txt)
C:\Users\wy.DESKTOP-KENPKKP\Desktop\Dingda\dingAPI>pip install -r requirements.txt Requirement already satisfied: requests==2.18.4 in c:\users\wy.desktop-kenpkkp\appdata\local\programs\python\python36\lib\site-p ackages (from -r requirements.txt (line 1)) (2.18.4) Requirement already satisfied: xlutils==2.0.0 in c:\users\wy.desktop-kenpkkp\appdata\local\programs\python\python36\lib\site-pac kages (from -r requirements.txt (line 2)) (2.0.0) Requirement already satisfied: xmltodict==0.12.0 in c:\users\wy.desktop-kenpkkp\appdata\local\programs\python\python36\lib\site- packages (from -r requirements.txt (line 3)) (0.12.0) Requirement already satisfied: xlrd==1.2.0 in
基本的命令选项如下:
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.
其中需注意,很可能遇到编码错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in
。需要指定编码格式“--encoding=utf8”。
在已生成依赖文件“requirements.txt”的情况下,它可以强行覆盖、比对差异以及清除不再使用的依赖项。