206

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?

0

6 Answers 6

311

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.

6
  • According to pypi.python.org/pypi/pipdeptree/0.3, pipdeptree | grep -P '^\w+' prints a requirements.txt.
    – MERose
    Commented Mar 9, 2016 at 8:59
  • @MERose Thanks for pointing that out. I must have still been using version 0.2 when I wrote this.
    – Anthon
    Commented Mar 9, 2016 at 9:15
  • It is not working for bokeh :( This package have specific organization of requirements so pip does not show them, but conda does.
    – Sklavit
    Commented Jul 18, 2017 at 15:47
  • It can now directly generate a requirements.txt directly using the --freeze flag Commented Sep 6, 2021 at 16:00
  • 1
    @sorin, I see a 2024 release on pypi... Commented Jan 19 at 23:13
13

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

11

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
    
3
  • 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)
    – ali_m
    Commented Jun 20, 2013 at 14:46
  • 5
    yolk doesn't have py3k support as of the time of writing.
    – yegle
    Commented Jan 13, 2015 at 22:45
  • Someone already did the port: pypi.org/project/yolk3k
    – vokimon
    Commented Jan 29, 2021 at 13:32
6

I realize that many years has passed since this question was asked, but it showed up in my searches so I thought I'd share some knowledge.

The pip-tools package contains a tool called pip-compile that seems to also solve the original poster's problem.

pip-compile takes an input file, which can be setup.py, setup.cfg, pyproject.toml, or requirements.in. The input file is what you write by hand and contains the "direct" dependencies. It may not specify exact dependency versions, but may use version ranges (nor no constraints at all). The tool outputs a new rquirements.txt file with all the indirect dependencies added and also pins down the dependencies to exact versions.

If you run the pip-compile tool again after updating the source file, it will add or remove dependencies from the output file if needed. You can also choose to upgrade a specific dependency by adding a flag.

So while pip-compile does not show you the dependency tree itself, it helps you with collecting all the leafs of the dependency tree (which I assume was what the original poster wanted to do in the end).

Read more here: https://github.com/jazzband/pip-tools/

0

Use pip-compile command to freeze requirements.in file

Pip-tools v6.14.0 seems to have pip-compile command to generate requirements.txt file with annotate by default. I see you just intend to freeze your installed package from virtualenv, but a better way is to have a setup.py or requirements.in file where you write your installed packages with the version.

Output will be similar to below:

# requirements.txt
# This file is autogenerated by pip-compile with Python 3.7
# by the following command:
#
#    pip-compile --no-emit-index-url --output-file requirements.txt setup.py
#
my-private-package==2.3.0
    # via my-repo (setup.py)
alembic==1.7.4
    # via my-private-package-dep
amqp==2.6.1
    # via kombu

You can also use --no-annotate arg to turn off dependencies annotation.

0

If you use poetry, you'll see the dependency tree in the poetry.lock file in the [package.dependencies] sections

For example:

[[package]]
name = "requests"
version = "2.31.0"
description = "Python HTTP for Humans."
optional = false
python-versions = ">=3.7"
files = [
    {file = "requests-2.31.0-py3-none-any.whl", hash = "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f"},
    {file = "requests-2.31.0.tar.gz", hash = "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1"},
]

[package.dependencies]
certifi = ">=2017.4.17"
charset-normalizer = ">=2,<4"
idna = ">=2.5,<4"
urllib3 = ">=1.21.1,<3"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.