[Distutils] A little bit of fun with package dependencies

Paul Moore p.f.moore at gmail.com
Wed May 10 11:25:42 EDT 2017


I was playing with pkg_resources today, and came up with the following
little script. It's nothing special, but I thought it might be of
interest. It uses pkg_resources and networkx to create a graph of
dependencies between the packages installed in your Python
environment.

The output is a GraphML file, that can be loaded into a graph display
tool (I used a tool called yEd). You could probably also output in
other formats, like GraphViz.

I don't know how interesting this will be to others, but I thought I'd
share it - enjoy!

Paul

import networkx as nx
from pkg_resources import Environment

e = Environment()
g = nx.DiGraph()
for name in sorted(e):
    dists = e[name]
    for dist in dists:
        reqs = dist.requires()
        for r in reqs:
            g.add_edge(dist.project_name, r.name)

for n in g.nodes():
    g.node[n]['Label'] = n

nx.write_graphml(g, 'deps.graphml')


More information about the Distutils-SIG mailing list