23

I have some dot files (digraphs) that I need to read in Python and extract some values from the nodes to populate my data structure. I see there are two graphviz packages for Python: graphviz and pygraphviz. Is there any big difference between the two? From a quick scroll of the docs, they pretty much seem to do the same thing. I'll be using this in Python 2.7.X for the aforementioned task.

John Y
  • 14,123
  • 2
  • 48
  • 72
user4979733
  • 3,181
  • 4
  • 26
  • 41
  • 1
    perhaps useful too: https://qr.ae/pGtb7r – Charlie Parker May 07 '21 at 00:18
  • 2
    perhaps also useful: https://github.com/pygraphviz/pygraphviz/issues/346 and https://python.libhunt.com/compare-pydot-vs-pygraphviz and https://github.com/pydot/pydot/issues/262 – Charlie Parker May 07 '21 at 00:25
  • 2
    I don't understand, why does pygraphviz and graphviz BOTH exist as python packages? What is pygraphviz doing that graphviz didn't already do? why would I choose pygraphviz vs graphviz for python? – Charlie Parker May 07 '21 at 16:31

1 Answers1

35

graphviz is a lightweight library which calls graphviz as a subprocess to execute all actions and produce output. This library is great as a quick and easy way to produce SVG or PNG output.

pygraphviz contains complete C bindings which uses graphviz as a library and expose all of graphviz's internal functionality like add/remove nodes/edges. But it comes with higher complexity in deployment as pip needs to compile C bindings and find all libraries.

In your case, as you need to read and manipulate dot files, it looks like you have to go with pygraphviz. Other interesting alternative to take a look is http://pypi.python.org/pypi/pydot which is a pure python dot parser.

Disclaimer: I am biased, because I contributed (a little bit) to pygraphviz.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Max Markov
  • 924
  • 12
  • 23
  • 8
    You may want to add a note for Anaconda users: `conda install graphviz` will install the graphviz command line tools (including `dot`), *not* the Python `graphviz` library. If you want the `graphviz` Python library, you must run `conda install python-graphviz`, which will also install the graphviz command line tools (and for the `pygraphviz` library, just run `conda install pygraphviz`). – MiniQuark Oct 13 '19 at 08:12
  • 4
    what about pygraphviz vs pydot? – Charlie Parker May 07 '21 at 00:18
  • 1
    I don't understand, why does pygraphviz and graphviz BOTH exist as python packages? What is pygraphviz doing that graphviz didn't already do? why would I choose pygraphviz vs graphviz for python? – Charlie Parker May 07 '21 at 16:31
  • 3
    @CharlieParker I think both are solving essentially same problem via different approaches. `graphviz` is pure python library for "dot file" manipulation and uses graphviz engine for rendering, `pygraphviz` is a wrapper on top of graphviz C library and uses graphviz for manipulating underlying object model as well. It passed 5 years since the original answer, and it looks like by now pretty much parity between packages was reached. I recommend to go with pure python `graphviz` as deployment without need to compile c bindings is so much easier. – Max Markov Nov 11 '21 at 00:28