Visualize python package dependencies with graphviz

What you want to do

Suppose you want to take a quick look at the dependencies of a python package. Here, we will explain how to visualize with graphviz. I have confirmed it on Ubuntu 15.10, but maybe it can be done on windows.

installation

Assumption: You have Anaconda installed.

The installation of graphviz is as follows. Install graphviz itself with conda and wrapper with pip. I also needed a library called libltdl7.

bash


conda install -y graphviz
pip install graphviz
apt-get install -y libltdl7

Use pipdeptree for package dependencies. The installation is as follows.

bash


pip install pipdeptree

Execute

You can create python-dep.png as follows. They are lined up so that you can install from the left.

python


import graphviz
from subprocess import run, DEVNULL, PIPE
ss = run(['pipdeptree'], stdout=DEVNULL, stderr=PIPE, 
         universal_newlines=True).stderr.rstrip().split('\n')
ss = [s[2:].lower().split()[:3] for s in ss if s[0] in ' *']
g = graphviz.Digraph(format='png', filename='python-dep', engine='dot')            
g.edges([(s[2], s[0][:s[0].index('=')]) for s in ss])
g.attr('graph', rankdir='LR')
g.render()

image

Try with Docker

The above series of docker (Dockerfile blob / master / Dockerfile)) is prepared. You can create python-dep.png as below.

bash


docker run -it --rm -v $PWD:/tmp -w /tmp \
    tsutomu7/python-dep python /root/python-dep.py

reference Summary of how to draw a graph in Graphviz and dot language Easy Python package management with pip related tools I tried to graph the packages installed in Python ... I found it after writing the article.

that's all

Recommended Posts