查找python项目依赖并生成requirements.txt

多人一起开发项目的时候总是要搭建环境和部署环境的,这个时候必须得有个python第三方包的list,一般都叫做requirements.txt。 如果一个项目使用时virtualenv环境,还好办 pip freeze 就可以解决,但是如果一个项目的依赖list没有维护,而且又是环境混用,那就不好整理。不过,这里推荐一个python模块pipreqs,可以自动根据源码生成 requirements.txt

使用pip freeze生成requirements.txt

 pip freeze > requirements.txt

使用 pipreqs

这个工具的好处是可以通过对项目目录的扫描,自动发现使用了那些类库,自动生成依赖清单。

缺点是可能会有些偏差,需要检查并自己调整下。

# pip install pipreqs

使用方式也比较简单

pipreqs ./

INFO: Starting new HTTPS connection (1): pypi.python.org
INFO: Starting new HTTPS connection (1): pypi.python.org
INFO: Starting new HTTPS connection (1): pypi.python.org
INFO: Starting new HTTPS connection (1): pypi.python.org
INFO: Starting new HTTPS connection (1): pypi.python.org
INFO: Starting new HTTPS connection (1): pypi.python.org
INFO: Successfully saved requirements file in ./requirements.txt

cat requirements.txt

chardet==2.0.1
colorama==0.2.5
configobj==4.7.2
Django==1.11.10
djangorestframework==3.7.7
gunicorn==19.7.1
html5lib==0.999
jieba==0.39
Landscape-Client==14.12
MySQL-python==1.2.3
numpy==1.14.0
PAM==0.4.2
pandas==0.22.0
Pillow==5.0.0
PyMySQL==0.8.0
pyOpenSSL==0.13
pyserial==2.6
python-apt===0.9.3.5ubuntu2
python-dateutil==2.6.1
python-debian===0.1.21-nmu2ubuntu2
python-igraph==0.6.5
pytz==2017.3
redis==2.10.6
requests==2.2.1
six==1.5.2
ssh-import-id==3.21
Twisted-Core==13.2.0
urllib3==1.7.1
zope.interface==4.0.5

原文网址:http://www.chenxm.cc/post/538…

    原文作者:风v月
    原文地址: https://segmentfault.com/a/1190000013201547
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞