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.

Sign up to request clarification or add additional context in comments.

6 Comments

looks reasonable, +1. sfoodxx -I /Library/...python2.6 (mac) doesn't filter out standard imports sys, os.path etc. ?
@Denis: Use sfood-filter-stdlib to remove entries from the Python standard library. I'll edit my answer to include this...
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.)
sfood-target-files is not available on Ubuntu 13.04
@Konstantin: That's odd. It is in Ubuntu 12.04 and also in 14.04. You might try the installation instructions per the documentation or upgrading to a supported version of Ubuntu.
|
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 Comment

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

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.)
and pprint can be used to pretty print the output produced by above snippet
get_installed_distributions is now deprecated.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.