Build a dependency graph in python Build a dependency graph in python python python

Build a dependency graph in python


Assuming your input from above is given as a string in raw:

import networkx as nximport reregex = re.compile(r'^([A-Z]+)::Requires\s+=\s([A-Z"]+)$')G = nx.DiGraph()roots = set()for l in raw.splitlines():    if len(l):        target, prereq = regex.match(l).groups()        if prereq == '""':            roots.add(target)        else:            G.add_edge(prereq, target)

Now print the tree(s):

for s in roots:    print s    spacer = {s: 0}    for prereq, target in nx.dfs_edges(G, s):        spacer[target] = spacer[prereq] + 2        print '{spacer}+-{t}'.format(                                     spacer=' ' * spacer[prereq],                                     t=target)    print ''

this prints:

A+-H+-B  +-CAA+-BB  +-CC

This requires that all roots are presented through root::Requires = "" in order for them to be identified as such.


Try with one of the several ones:

graph-tool is very difficult to install (it needs a lot of memory for compilation, I think it was around 5GB of RAM and around 12 hours of compilation).

networkx is pretty decent.

igraph quote from their page: igraph is a free software package for creating and manipulating undirected and directed graphs. It includes implementations for classic graph theory problems like minimum spanning trees and network flow, and also implements algorithms for some recent network analysis methods, like community structure search.

I've been using all of them. It really depends on what exactly do you need. If you need them for something as simple as dependencies, then it really is not important which one you are going to use, though, I would recomend you to avoud graph-tool if you need it for something shorter and lighter.


Graphviz is great for building documentation of dependencies in an automated manner.

There's a useful Python library too called pygraphviz

I use this to build up a dependency map then output in both text form and as a visual that automatically exports to PDF.