List Dependencies in Python

List dependencies in Python

Scan your import statements. Chances are you only import things you explicitly wanted to import, and not the dependencies.

Make a list like the one pip freeze does, then create and activate a virtualenv.

Do pip install -r your_list, and try to run your code in that virtualenv. Heed any ImportError exceptions, match them to packages, and add to your list. Repeat until your code runs without problems.

Now you have a list to feed to pip install on your deployment site.

This is extremely manual, but requires no external tools, and forces you to make sure that your code runs. (Running your test suite as a check is great but not sufficient.)

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

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'"]

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.

How can I ask setup.py to list dependencies?

python setup.py egg_info will write a package_name.egg-info/requires.txt file which contains the dependencies you want.

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.



Related Topics



Leave a reply



Submit