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

55,781

Solution 1

You should take a look at 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.txt file 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:

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

The 0.5 version of pipdeptree also allows JSON output with the --json option, that is more easily machine parseble, at the expense of being less readable.

Solution 2

Warning: py2 only / abandonware

yolk can display dependencies for packages, provided that they

  • 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
    

Solution 3

You can do it by installing pipdeptree package.

Open command prompt in your project folder. If you are using any virtual environment, then switch to that virtual environment.

Install pipdeptree package using pip

pip install pipdeptree
pipdeptree -fl

This package will list all the dependencies of your project.

For more pipdeptree

enter image description here

Share:
55,781
tbicr
Author by

tbicr

Updated on August 05, 2021

Comments

  • tbicr
    tbicr over 2 years

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

    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:

    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?

  • tbicr
    tbicr almost 11 years
    Thanks. This not full solution, but however helpful utility.
  • ali_m
    ali_m almost 11 years
    I'm not sure if there can be a full solution - the problem is that dependency information doesn't always exist (for example for packages installed via distutils, which does not support package metadata)
  • yegle
    yegle over 9 years
    yolk doesn't have py3k support as of the time of writing.
  • MERose
    MERose about 8 years
    According to pypi.python.org/pypi/pipdeptree/0.3, pipdeptree | grep -P '^\w+' prints a requirements.txt.
  • Anthon
    Anthon about 8 years
    @MERose Thanks for pointing that out. I must have still been using version 0.2 when I wrote this.
  • Sklavit
    Sklavit almost 7 years
    It is not working for bokeh :( This package have specific organization of requirements so pip does not show them, but conda does.
  • vokimon
    vokimon over 3 years
    Someone already did the port: pypi.org/project/yolk3k
  • Wasi Master
    Wasi Master over 2 years
    It can now directly generate a requirements.txt directly using the --freeze flag