Skip to content

Instantly share code, notes, and snippets.

@jbgo
Created August 3, 2011 19:38
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgo/1123577 to your computer and use it in GitHub Desktop.
Save jbgo/1123577 to your computer and use it in GitHub Desktop.
python dependency graph generator

python dependency graph generator

Generates a dependency graph for a list of installed python packages in the form of a dot file for graphviz.

Usage

python dependency-graph.py installed_package_name ... | dot -Tpng -o dependencies.png
import sys
import pkg_resources
def dependencies_for(project_name):
dist = pkg_resources.get_distribution(project_name)
return [r.project_name for r in dist.requires()]
printed_dependencies = {}
lib_dependencies = []
def print_dependencies(lib_name):
global lib_dependencies
printed_dependencies[lib_name] = True
deps = dependencies_for(lib_name)
for dep in deps:
lib_dependency = ' "%s" -> "%s";' % (lib_name, dep)
if not lib_dependency in lib_dependencies:
print lib_dependency
if not dep in printed_dependencies: # avoid circular dependencies
print_dependencies(dep)
lib_dependencies.append(lib_dependency)
def print_digraph(libs):
print 'digraph ipy_dependencies {'
print ' rankdir=LR;'
print ' truecolor=true;'
for lib in libs:
print_dependencies(lib)
print '}'
if __name__ == '__main__':
print_digraph(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment