list python package dependencies without loading them?

Posted on

Question :

list python package dependencies without loading them?

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/,
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:

Asked By: denis


Answer #1:


sfood -fuq | 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 | 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.

Answered By: unutbu

Answer #2:

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. 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

Other hints about the use of pylint or Gui2exe here

Answered By: joaquin

Answer #3:

If by package you mean a pip installed package (and not a directory with an, 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()

Leave a Reply

Your email address will not be published. Required fields are marked *