Python 有没有办法显示pip包的依赖树?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17194301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:44:05  来源:igfitidea点击:

Is there any way to show the dependency trees for pip packages?

pythonpiprequirements.txt

提问by tbicr

I have a project with multiple package dependencies, the main requirements being listed in requirements.txt. When I call pip freezeit prints the currently installed packages as plain list. I would prefer to also get their dependency relationships, something like this:

我有一个具有多个包依赖项的项目,主要要求列在requirements.txt. 当我调用pip freeze它时,它会将当前安装的软件包打印为普通列表。我更希望获得他们的依赖关系,如下所示:

Flask==0.9
    Jinja2==2.7
    Werkzeug==0.8.3

Jinja2==2.7

Werkzeug==0.8.3

Flask-Admin==1.0.6
    Flask==0.9
    Jinja2==2.7
    Werkzeug==0.8.3

The goal is to detect the dependencies of each specific package:

目标是检测每个特定包的依赖关系:

Werkzeug==0.8.3
    Flask==0.9
    Flask-Admin==1.0.6

And insert these into my current requirements.txt. For example, for this input:

并将这些插入到我当前的requirements.txt. 例如,对于这个输入:

Flask==0.9
Flask-Admin==1.0.6
Werkzeug==0.8.3

I would like to get:

我想得到:

Flask==0.9
    Jinja2==2.7
Flask-Admin==1.0.6
Werkzeug==0.8.3

Is there any way show the dependencies of installed pip packages?

有没有办法显示已安装的pip包的依赖关系?

采纳答案by Anthon

You should take a look at pipdeptree:

你应该看看pipdeptree

$ pip install pipdeptree
$ pipdeptree -fl
Warning!!! Cyclic dependencies found:
------------------------------------------------------------------------
xlwt==0.7.5
ruamel.ext.rtf==0.1.1
xlrd==0.9.3
openpyxl==2.0.4
  - jdcal==1.0
pymongo==2.7.1
reportlab==3.1.8
  - Pillow==2.5.1
  - pip
  - setuptools

It doesn't generate a requirements.txtfile as you indicated directly. However the source (255 lines of python code) should be relatively easy to modify to your needs, or alternatively you can (as @MERose indicated is in the pipdeptree 0.3 README ) out use:

它不会requirements.txt像您直接指出的那样生成文件。但是,源代码(255 行 python 代码)应该相对容易根据您的需要进行修改,或者您也可以(如@MERose 在 pipdeptree 0.3 README 中指出的那样)使用:

pipdeptree --freeze  --warn silence | grep -P '^[\w0-9\-=.]+' > requirements.txt

The 0.5 version of pipdeptreealso allows JSON output with the --jsonoption, that is more easily machine parseble, at the expense of being less readable.

0.5 版本pipdeptree还允许带有--json选项的JSON 输出,这更容易机器解析,但代价是可读性较低。

回答by ali_m

Warning: py2 only / abandonware

警告:仅限 py2/废弃软件

yolkcan display dependencies for packages, provided that they

yolk可以显示包的依赖关系,前提是它们

  • were installed via setuptools
  • came with metadata that includes dependency information

    $ yolk -d Theano
    Theano 0.6.0rc3
      scipy>=0.7.2
      numpy>=1.5.0
    
  • 通过安装 setuptools
  • 附带包含依赖信息的元数据

    $ yolk -d Theano
    Theano 0.6.0rc3
      scipy>=0.7.2
      numpy>=1.5.0