Is there a way to list pip dependencies/requirements?

PythonPiprequirements.txt

Python Problem Overview


Without going through with the installation, I want to quickly see all the packages that pip install would install.

Python Solutions


Solution 1 - Python

Check out my project johnnydep!

Installation:

pip install johnnydep

Usage example:

$ johnnydep requests
name                       summary
-------------------------  ----------------------------------------------------------------------
requests                   Python HTTP for Humans.
├── certifi>=2017.4.17     Python package for providing Mozilla's CA Bundle.
├── chardet<3.1.0,>=3.0.2  Universal encoding detector for Python 2 and 3
├── idna<2.7,>=2.5         Internationalized Domain Names in Applications (IDNA)
└── urllib3<1.23,>=1.21.1  HTTP library with thread-safe connection pooling, file post, and more.

A more complex tree:

$ johnnydep ipython 
name                              summary
--------------------------------  -----------------------------------------------------------------------------
ipython                           IPython: Productive Interactive Computing
├── appnope                       Disable App Nap on OS X 10.9
├── decorator                     Better living through Python with decorators
├── jedi>=0.10                    An autocompletion tool for Python that can be used for text editors.
│   └── parso==0.1.1              A Python Parser
├── pexpect                       Pexpect allows easy control of interactive console applications.
│   └── ptyprocess>=0.5           Run a subprocess in a pseudo terminal
├── pickleshare                   Tiny 'shelve'-like database with concurrency support
├── prompt-toolkit<2.0.0,>=1.0.4  Library for building powerful interactive command lines in Python
│   ├── six>=1.9.0                Python 2 and 3 compatibility utilities
│   └── wcwidth                   Measures number of Terminal column cells of wide-character codes
├── pygments                      Pygments is a syntax highlighting package written in Python.
├── setuptools>=18.5              Easily download, build, install, upgrade, and uninstall Python packages
├── simplegeneric>0.8             Simple generic functions (similar to Python's own len(), pickle.dump(), etc.)
└── traitlets>=4.2                Traitlets Python config system
    ├── decorator                 Better living through Python with decorators
    ├── ipython-genutils          Vestigial utilities from IPython
    └── six                       Python 2 and 3 compatibility utilities

Solution 2 - Python

This was tested with pip versions 8.1.2, 9.0.1, 10.0.1, and 18.1.

To get the output without cluttering your current directory on Linux use

pip download [package] -d /tmp --no-binary :all: -v

-d tells pip the directory that download should put files in.

Better, just use this script with the argument being the package name to get only the dependencies as output:

#!/bin/sh

PACKAGE=$1
pip download $PACKAGE -d /tmp --no-binary :all:-v 2>&1 \
| grep Collecting \
| cut -d' ' -f2 \
| grep -Ev "$PACKAGE(~|=|\!|>|<|$)"

Also available here.

Solution 3 - Python

If and only if the package is install, you can use pip show <package>. Look for the Requires: filed at the end of the output. Clearly, this breaks your requirement but might be useful nonetheless.

For example:

$ pip --version
pip 7.1.0 [...]
$ pip show pytest
---
Metadata-Version: 2.0
Name: pytest
Version: 2.7.2
Summary: pytest: simple powerful testing with Python
Home-page: http://pytest.org
Author: Holger Krekel, Benjamin Peterson, Ronny Pfannschmidt, Floris Bruynooghe and others
Author-email: holger at merlinux.eu
License: MIT license
Location: /home/usr/.tox/develop/lib/python2.7/site-packages
Requires: py

Solution 4 - Python

>Note: the feature used in this answer was deprecated in 2014 and removed in 2015. Please see other answers that apply to modern pip.

The closest you can get with pip directly is by using the --no-install argument:

pip install --no-install <package>

For example, this is the output when installing celery:

Downloading/unpacking celery                                                                                   
  Downloading celery-2.5.5.tar.gz (945Kb): 945Kb downloaded
  Running setup.py egg_info for package celery

    no previously-included directories found matching 'tests/*.pyc'
    no previously-included directories found matching 'docs/*.pyc'
    no previously-included directories found matching 'contrib/*.pyc'
    no previously-included directories found matching 'celery/*.pyc'
    no previously-included directories found matching 'examples/*.pyc'
    no previously-included directories found matching 'bin/*.pyc'
    no previously-included directories found matching 'docs/.build'
    no previously-included directories found matching 'docs/graffles'
    no previously-included directories found matching '.tox/*'
Downloading/unpacking anyjson>=0.3.1 (from celery)
  Downloading anyjson-0.3.3.tar.gz
  Running setup.py egg_info for package anyjson

Downloading/unpacking kombu>=2.1.8,<2.2.0 (from celery)
  Downloading kombu-2.1.8.tar.gz (273Kb): 273Kb downloaded
  Running setup.py egg_info for package kombu

Downloading/unpacking python-dateutil>=1.5,<2.0 (from celery)
  Downloading python-dateutil-1.5.tar.gz (233Kb): 233Kb downloaded
  Running setup.py egg_info for package python-dateutil

Downloading/unpacking amqplib>=1.0 (from kombu>=2.1.8,<2.2.0->celery)
  Downloading amqplib-1.0.2.tgz (58Kb): 58Kb downloaded
  Running setup.py egg_info for package amqplib

Successfully downloaded celery anyjson kombu python-dateutil amqplib

Admittedly, this does leave some cruft around in the form of temporary files, but it does accomplish the goal. If you're doing this with virtualenv (which you should be), the cleanup is as easy as removing the <virtualenv root>/build directory.

Solution 5 - Python

I quote an alternative solution from @onnovalkering:

> PyPi provides a JSON endpoint with package metadata: > > >>> import requests > >>> url = 'https://pypi.org/pypi/{}/json'; > >>> json = requests.get(url.format('pandas')).json() > >>> json['info']['requires_dist'] > ['numpy (>=1.9.0)', 'pytz (>=2011k)', 'python-dateutil (>=2.5.0)'] > >>> json['info']['requires_python'] > '>=2.7,!=3.0.,!=3.1.,!=3.2.,!=3.3.,!=3.4.*' > > For a specific package version, add an additional version segment to > the URL: > > https://pypi.org/pypi/pandas/0.22.0/json

Also if you are using conda (as suggested by @ShpielMeister), you can use:

conda info package==X.X.X

to display information, including dependencies for a particular version or:

conda info package

to display information, including dependencies about all supported versions of that package.

Solution 6 - Python

Use pipdeptree ( pip install pipdeptree). Needs the package to be installed.

$ pipdeptree -p pandas
pandas==1.2.2
  - numpy [required: >=1.16.5, installed: 1.19.5]
  - python-dateutil [required: >=2.7.3, installed: 2.8.1]
    - six [required: >=1.5, installed: 1.15.0]
  - pytz [required: >=2017.3, installed: 2021.1]

Use johnnydep (pip install johnnydep). Slower because it download the wheels of the packages.

$ johnnydep pandas
2021-06-09 11:01:21 [info     ] init johnnydist                [johnnydep.lib] dist=pandas parent=None
2021-06-09 11:01:22 [info     ] init johnnydist                [johnnydep.lib] dist=numpy>=1.16.5 parent=pandas
2021-06-09 11:01:22 [info     ] init johnnydist                [johnnydep.lib] dist=python-dateutil>=2.7.3 parent=pandas
2021-06-09 11:01:23 [info     ] init johnnydist                [johnnydep.lib] dist=pytz>=2017.3 parent=pandas
2021-06-09 11:01:23 [info     ] init johnnydist                [johnnydep.lib] dist=six>=1.5 parent=python-dateutil>=2.7.3
name                        summary
--------------------------  -----------------------------------------------------------------------
pandas                      Powerful data structures for data analysis, time series, and statistics
├── numpy>=1.16.5           NumPy is the fundamental package for array computing with Python.
├── python-dateutil>=2.7.3  Extensions to the standard Python datetime module
│   └── six>=1.5            Python 2 and 3 compatibility utilities
└── pytz>=2017.3            World timezone definitions, modern and historical

Solution 7 - Python

I think these answers are outdated and there's a better solution now. Original post here:

To generate requirements.txt for packages listed in install_requires in your setup.cfg or setup.py, you would need to install pip-tools.

pip install pip-tools
pip-compile

To generate a requirements.txt file that includes packages specified under extras_requires for tests and dev:

pip-compile --extra tests --extra devrequirements.txt file with packages listed under

Furthermore, you can also use requirements.in file instead of setup.cfg or setup.py to list your requirements.

pip-compile requirements.in

Solution 8 - Python

Just an addendum on how to use johnnydep.

If you want to have the dependencies in requirements.txt format:

johnnydep pandas --output-format=pinned
pandas==1.4.1
numpy==1.22.3
python-dateutil==2.8.2
pytz==2021.3
six==1.16.0

Above list will be written to stdout while the informational messages during collection are written to stderr (if you want to capture the output using bash or subprocess).

Calling it from Python instead of the CLI:

import sys, johnnydep.cli
sys.argv = ["", "pandas"]
johnnydep.cli.main()

Solution 9 - Python

The command pip install <package> --download <path> should be used, as mentioned in comments by @radtek, since as of 7.0.0 (2015-05-21), --no-install is removed from pip. This will download the dependencies needed into <path>.

Solution 10 - Python

Another option is to use a helper script similar to this one which uses the pip.req.parse_requirements API to parse requirements.txt files and a distutils.core.setup replacement to parse setup.py files.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlastoneisbearfoodView Question on Stackoverflow
Solution 1 - PythonwimView Answer on Stackoverflow
Solution 2 - PythonJmillsView Answer on Stackoverflow
Solution 3 - PythonSardathrion - against SE abuseView Answer on Stackoverflow
Solution 4 - PythonTravis MehlingerView Answer on Stackoverflow
Solution 5 - PythonpgmankView Answer on Stackoverflow
Solution 6 - PythonFlorianView Answer on Stackoverflow
Solution 7 - PythonrrlamichhaneView Answer on Stackoverflow
Solution 8 - PythonxjclView Answer on Stackoverflow
Solution 9 - PythontoineView Answer on Stackoverflow
Solution 10 - PythonsschuberthView Answer on Stackoverflow