首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python从pip冻结的输出中了解基本需求。

Python从pip冻结的输出中了解基本需求。
EN

Stack Overflow用户
提问于 2019-01-21 14:37:46
回答 1查看 1.3K关注 0票数 5

我的朋友刚开始学习Python和Flask,缺少了很多“最佳实践”,比如requirements.txt文件。

他最近要求我提供帮助,为了使项目整洁,我想建立一个CI服务(Travis),但我需要首先处理这个文件。

因为他最初没有requirements.txt,所以我能得到的所有信息都是他的import语句,以及他的pip freeze输出。

由于无法区分项目的直接需求和包中的间接需求,我想从列表中找出所有“顶级”包。“顶级包”是列表中的另一个包不需要的包。例如,urllib3requests所必需的,所以当requests出现时,urllib3最好不要出现在最终结果中。

有办法做到这一点吗?

如果有人想帮助我处理这个特定的实例,下面是pip freeze的输出

代码语言:javascript
复制
apturl==0.5.2
arrow==0.12.1
asn1crypto==0.24.0
binaryornot==0.4.4
blinker==1.4
Bootstrap-Flask==1.0.9
Brlapi==0.6.6
certifi==2018.1.18
chardet==3.0.4
Click==7.0
colorama==0.3.7
command-not-found==0.3
configparser==3.5.0
cookiecutter==1.6.0
cryptography==2.1.4
cupshelpers==1.0
decorator==4.1.2
defer==1.0.6
distro-info==0.18
dominate==2.3.5
Flask==1.0.2
Flask-Bootstrap4==4.0.2
Flask-Login==0.4.1
Flask-Mail==0.9.1
Flask-Moment==0.6.0
Flask-SQLAlchemy==2.3.2
Flask-WTF==0.14.2
future==0.17.1
httpie==0.9.8
httplib2==0.9.2
idna==2.6
ipython==5.5.0
ipython-genutils==0.2.0
itsdangerous==1.1.0
Jinja2==2.10
jinja2-time==0.2.0
keyring==10.6.0
keyrings.alt==3.0
language-selector==0.1
launchpadlib==1.10.6
lazr.restfulclient==0.13.5
lazr.uri==1.0.3
louis==3.5.0
macaroonbakery==1.1.3
Mako==1.0.7
MarkupSafe==1.1.0
mysqlclient==1.3.14
netifaces==0.10.4
oauth==1.0.1
olefile==0.45.1
pexpect==4.2.1
pickleshare==0.7.4
Pillow==5.1.0
poyo==0.4.2
prompt-toolkit==1.0.15
protobuf==3.0.0
pycairo==1.16.2
pycrypto==2.6.1
pycups==1.9.73
Pygments==2.2.0
pygobject==3.26.1
pymacaroons==0.13.0
PyNaCl==1.1.2
pyRFC3339==1.0
python-apt==1.6.3
python-dateutil==2.7.5
python-debian==0.1.32
pytz==2018.3
pyxdg==0.25
PyYAML==3.12
reportlab==3.4.0
requests==2.18.4
requests-unixsocket==0.1.5
ruamel.yaml==0.15.34
SecretStorage==2.3.1
simplegeneric==0.8.1
simplejson==3.13.2
six==1.11.0
SQLAlchemy==1.2.14
system-service==0.3
systemd-python==234
traitlets==4.3.2
ubuntu-drivers-common==0.0.0
ufw==0.35
unattended-upgrades==0.1
urllib3==1.22
usb-creator==0.3.3
visitor==0.1.3
wadllib==1.3.2
wcwidth==0.1.7
Werkzeug==0.14.1
whichcraft==0.5.2
WTForms==2.2.1
xkit==0.0.0
zope.interface==4.3.2

这是import语句,还有一个附加的pymysql,他告诉我。

代码语言:javascript
复制
import os
from flask import *
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_wtf import FlaskForm
from wtforms import *
from wtforms.validators import *
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail, Message
from werkzeug.security import generate_password_hash,check_password_hash
from flask_login import login_required , login_user,login_fresh,login_url,LoginManager,UserMixin,logout_user
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-21 17:32:57

首先,我建议使用pip的API,但建议只使用pip作为cmdline工具([PyPA]:使用程序中的pip)。请注意,我成功地使用了它,只是没有公开代码(至少目前是这样)。

下面是--一种使用pkg_resources (资源)的方法。

code.py

代码语言:javascript
复制
#!/usr/bin/env python3

import sys
import os
import pkg_resources


def get_pkgs(reqs_file="requirements_orig.txt"):
    if reqs_file and os.path.isfile(reqs_file):
        ret = dict()
        with open(reqs_file) as f:
            for item in f.readlines():
                name, ver = item.strip("\n").split("==")[:2]
                ret[name] = ver, ()
        return ret
    else:
        return {
                   item.project_name: (item.version, tuple([dep.name for dep in item.requires()])) for item in pkg_resources.working_set
               }


def print_pkg_data(text, pkg_info):
    print("{:s}\nSize: {:d}\n\n{:s}".format(text, len(pkg_info), "\n".join(["{:s}=={:s}".format(*item) for item in pkg_info])))


def main():
    pkgs = get_pkgs(reqs_file=None)
    full_pkg_info = [(name, data[0]) for name, data in sorted(pkgs.items())]
    print_pkg_data("----------FULL LIST----------", full_pkg_info)

    deps = set()
    for name in pkgs:
        deps = deps.union(pkgs[name][1])
    min_pkg_info = [(name, data[0]) for name, data in sorted(pkgs.items()) if name not in deps]
    print_pkg_data("\n----------MINIMAL LIST----------", min_pkg_info)


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()

输出

(py_064_03.06.08_test0) e:\Work\Dev\StackOverflow\q054292236>"e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe“code.py Python3.6.8(tag/v3.6.8:3c6b436a57)00:16:47) MSC v.1916 64位(AMD64) on win325.3 QtPy==1.5.2 Send2Trash==1.5.0 Sphinx==1.8.3 Werkzeug==0.14.1 absl-py=0.6.1 alabaster==0.7.12 asn1crypto==0.24.0 astor==0.7.1 astroid==2.1.0 backcall==0.1.0 bleach==3.0.2 certifi==2018.11.29 cffi==1.11.5 chardet==3.0.4 cloudpickle==0.6.1 colorama==0.4.1 cryptography==2.4.2 cycler==0.10.0 decorator==4.3.0 en21 20 en22#。5 funcsigs==1.0.2 future==0.17.1 gast==0.2.0 grpcio==1.17.1 h5py==2.9.0 html5lib==1.0.1 idna==2.8 imagesize==1.1.0 ipaddr==2.2.0 ipykernel==5.1.0 ipython==7.2.0 ipython-genutils=0.2.0 ipywidgets==7.4.2 isort==4.3.4 itsdangerous==1.1.0 jedi==0.13.2 jsonschema==2.6.0 jupyter==1.0.0 jupyter-client==5.2.4 jupyter-console=6.0.0 jupyter-core=4.4.0 keyboard==0.13.2 jedi==0.13.2 jupyter.1 kiwisolver==1.0.1惰性-对象-代理=1.3.1 llvmlite==0.26.0 lxml==4.2.5 matplotlib==3.0.2 mccabe==0.6.1 mistune==0.8.4 nbconvert==5.4.0 nbformat==4.4.0 notebook==5.7.4 numba==0.41.0 numpy==1.15.4 numpydoc==0.8.0 opencv-python=3.4.19 packaging==18.0 pandas==0.23.4 pandocfilters==1.4.2 parso==0.3.1 patsy==0.5.1 prometheus-client=0.5.0提示-toolkit==2.0.7 protobuf==3.6.1 psutil==5.4.8 pyOpenSSL==18.0.0 pycodestyle==2.4.0 pycparser==2.19 pycryptodome==3.7.2 pyflakes==2.0.0 pygame==1.9.4 pylint==2.2.2 pynput==1.4 pyparsing==2.3.0 python-dateutil=2.7.5 pytz==2018.7 pywin32==224 pywin32-ctype=0.2.0 pywinpty==0.5.5 pyzmq==17.1.2 qtconsole==4.4.3 requests==2.21.0 en22 20 en22.12.0 snowballstemmer==1.2.1 sphinxcontrib=1.1.0 spyder==3.3.2 spyder-kernels=0.3.0 statsmodels==0.9.0 tensorboard==1.12.1 tensorflow-gpu=1.12.0 tensorflow-tensorboard=1.5.1 termcolor==1.1.0 terminado==0.8.1 testpath==0.4.2 thrift==0.11.0 tornado==5.1.1 traitlets==4.3.2 typed ast=1.1.1 urllib3==1.24.1 wcwidth==0.1.7 webencodings==0.5.1 wheel==0.32.3 widgetsnbextension==3.4.2 widgetsnbextension==3.4.2 xlrd==1.2.0ipaddr==2.2.0 keyboard==0.13.2 lxml==4.2.5 opencv-python==3.4.4.19 pandas==0.23.4 patsy==0.5.1 pip==18.1 pyOpenSSL==18.0.0 pycryptodome==3.7.2 pygame==1.9.4 pynput==1.4 pywin32==224 spyder==3.3.2 statsmodels==0.9.0 tensorflow-gpu=1.12.0 tensorflow-tensorboard=1.5.1 thrift==0.11.0 xlrd==1.2.0

Notes

  • (说明显而易见):为了获取pkg信息,需要安装pkg。这就是为什么在我的示例中,我没有使用您的文件(我将其命名为requirements_orig.txt),而是在我的_VEnv上安装了pkg_s
  • 如您所见,在我的例子中,pkg数从133下降到37,我认为这是相当容易管理的(当然,还可以进行更多的过滤)。
  • 我创建数据结构的前提是,pkg名称是主键(唯一标识pkg)。如果这是错误的,则代码将需要进行一些更改。

最后注意事项:如果您还想考虑模块的导入列表(如果可能的话,以去掉更多的pkg_s ),您也可以尝试[Python 3]:modulefinder -查找脚本使用的模块 (我在初始化为运行?(@CristiFati的答案)中使用了它,仅从_cmdline中使用,但是从脚本中使用它应该很简单)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54292236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档