23

Say that python package A requires B, C and D; is there a way to list A → B C D without loading them ?
Requires in the metadata (yolk -M A) are often incomplete, grr.
One can download A.tar / A.egg, then look through A/setup.py, but some of those are pretty gory.

(I'd have thought that getting at least first-level dependencies could be mechanized; even a 98 % solution would be better than avalanching downloads.)

A related question: pip-upgrade-package-without-upgrading-dependencies

3 Answers 3

31

Snakefood

sfood -fuq package.py | sfood-target-files 

will list the dependencies.

`-f` tells sfood to follow dependencies recursively
`-u` tells sfood to ignore unused imports
`-q` tells sfood to be quiet about debugging information

To filter out modules from the standard library, you could use

sfood -fuq package.py | sfood-filter-stdlib | sfood-target-files 

As you've already noted, if there are other directories you'd like ignored, you can also use the sfood -I flag.

7
  • looks reasonable, +1. sfoodxx -I /Library/...python2.6 (mac) doesn't filter out standard imports sys, os.path etc. ?
    – denis
    May 21, 2010 at 15:59
  • @Denis: Use sfood-filter-stdlib to remove entries from the Python standard library. I'll edit my answer to include this...
    – unutbu
    May 21, 2010 at 16:26
  • 1
    sfood-filter-stdlib has '/usr/lib/python' hardwired in, and doesn't work for sfood-imports. Am asking the author ... Bytheway Kevin Teague, "I want a pony" groups.google.com/group/django-developers/msg/5407cdb400157259 is an outstanding overview of packaging methods in 2008.)
    – denis
    May 24, 2010 at 12:02
  • @Denis: Thanks; that's a great link.
    – unutbu
    May 24, 2010 at 14:38
  • sfood-target-files is not available on Ubuntu 13.04
    – Konstantin
    Jul 17, 2015 at 8:28
13

modulefinder from the standard lib

New in version 2.3.

This module provides a ModuleFinder class that can be used to determine the set of modules imported by a script. modulefinder.py can also be run as a script, giving the filename of a Python script as its argument, after which a report of the imported modules will be printed.

I am not sure if it complies with your requierement about not loading the modules. From here:

modulefinder use bytecode inspection to find dependencies, and therefore is free from any side-effects that may be caused by importing the modules being studied.

Other hints about the use of pylint or Gui2exe here

1
  • 3
    modulefinder does work but it will show a lot of useless stuff and indirect dependencies (dependencies that MAY be used by your dependencies, but actually are not). You can find an example script I did using modulefinder here: gist.github.com/lrq3000/6175634 And an alternative script using an AST parsing approach here (and which works way better for me): gist.github.com/lrq3000/6175522
    – gaborous
    Aug 7, 2013 at 16:31
6

If by package you mean a pip installed package (and not a directory with an __init__.py), then you can use the Python package called pip. For example:

def get_all_package_dependencies():
    """Return dictionary of installed packages to list of package dependencies."""
    return {
        dist.key: [r.key for r in dist.requires()]
        for dist in pip.get_installed_distributions()
    }
3
  • I see a package on the web and want to know its deps. There's pip install --no-deps, then dig around ... or are there newer pip commands to list-deps ? (Requirements files are most often just not there.)
    – denis
    Aug 6, 2013 at 10:07
  • and pprint can be used to pretty print the output produced by above snippet
    – binithb
    Jul 28, 2017 at 12:16
  • 1
    get_installed_distributions is now deprecated.
    – Amit Naidu
    Dec 18, 2019 at 2:43

Your Answer

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

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