Is there a way to list pip dependencies/requirements?
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.
Identifying the dependency relationship for python packages installed with pip
You could try pipdeptree which displays dependencies as a tree structure e.g.:
$ pipdeptree
Lookupy==0.1
wsgiref==0.1.2
argparse==1.2.1
psycopg2==2.5.2
Flask-Script==0.6.6
- Flask [installed: 0.10.1]
- Werkzeug [required: >=0.7, installed: 0.9.4]
- Jinja2 [required: >=2.4, installed: 2.7.2]
- MarkupSafe [installed: 0.18]
- itsdangerous [required: >=0.21, installed: 0.23]
alembic==0.6.2
- SQLAlchemy [required: >=0.7.3, installed: 0.9.1]
- Mako [installed: 0.9.1]
- MarkupSafe [required: >=0.9.2, installed: 0.18]
ipython==2.0.0
slugify==0.0.1
redis==2.9.1
To get it run:
pip install pipdeptree
EDIT: as noted by @Esteban in the comments you can also list the tree in reverse with -r
or for a single package with -p <package_name>
so to find what installed Werkzeug you could run:
$ pipdeptree -r -p Werkzeug
Werkzeug==0.11.15
- Flask==0.12 [requires: Werkzeug>=0.7]
How to list dependencies for a python library without installing?
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
Listing the dependencies of a package using pip
Note that this answer from 2012 is out of date. First, the workaround, which the answer already said you probably shouldn't do in 2012, now you can't do it. If you want a similar workaround, you could use pip download
, but it's even less likely to be what you want. Especially since pip show
has been improved. Fortunately, the question has been marked as a dup of a later question, so there's no reason to read this answer except for historical purposes.
You can't, at least not directly.
You can import the pip module in your own code and download the requirements file and then iterate through it. Or, from the command line, you can pip install --no-install --verbose
.
But really, unless this is something you need to automate, it's probably easier to just go to http://pypi.python.org/ and search for the package there instead of using pip.
List dependencies of Python wheel file
As previously mentioned, .whl
files are just ZIP archives. You can just open them and poke around in the METADATA
file.
There is a tool, however, that can make this manual process a bit easier. You can use pkginfo, which can be installed with pip.
CLI usage:
$ pip install pkginfo
$ pkginfo -f requires_dist psutil-5.4.5-cp27-none-win32.whl
requires_dist: ["enum34; extra == 'enum'"]
API usage:
>>> import pkginfo
>>> wheel_fname = "psutil-5.4.5-cp27-none-win32.whl"
>>> metadata = pkginfo.get_metadata(wheel_fname)
>>> metadata.requires_dist
[u"enum34 ; extra == 'enum'"]
Is there any way to show the dependency trees for pip packages?
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.
Related Topics
How to Get a Complete List of Object's Methods and Attributes
Sqlalchemy Create_All() Does Not Create Tables
Sorting a Dictionary by Value Then Key
What Is This Odd Colon Behavior Doing
Generate a Random Derangement of a List
Differencebetween Using Loc and Using Just Square Brackets to Filter for Columns in Pandas/Python
Python: the _Imagingft C Module Is Not Installed
Generate All Permutations of a List Without Adjacent Equal Elements
Why Does This Not Work as an Array Membership Test
How to Catch a Numpy Warning Like It's an Exception (Not Just for Testing)
How to Increment a Shared Counter from Multiple Processes
How to Make a 4D Plot with Matplotlib Using Arbitrary Data
Get the Name of a Pandas Dataframe
Bin Elements Per Row - Vectorized 2D Bincount for Numpy
How to Detect the Python Version at Runtime
Merge Multiple Column Values into One Column in Python Pandas