5

I just want to draw a simple graph with Python 3 networkx & graphviz:

import networkx as nx

G = nx.complete_graph(3)
nx.draw_graphviz(G)

I'm using ubuntu14.04 and IPython3 and as usual I did pip3 install networkx and running the code gave me:

ImportError: pydot could not be loaded: http://code.google.com/p/pydot/

And I tried installing pydotplus and running the code:

/usr/local/lib/python3.4/dist-packages/networkx/drawing/nx_pydot.py in pydot_layout(G, prog, root, **kwds)
    294 
    295         if isinstance(node,list):
--> 296             node=node[0]
    297         pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
    298         if pos != None:

IndexError: list index out of range

and pydot2 also:

/usr/local/lib/python3.4/dist-packages/pydot.py in write(self, path, prog, format)
   1893             prog = self.prog
   1894 
-> 1895         dot_fd = file(path, "w+b")
   1896         if format == 'raw':
   1897             data = self.to_string()

NameError: name 'file' is not defined

I spent quite some time searching and installing other pydots and pygraphviz combinations already but still no luck.

While this may be related: pydot and graphviz error: Couldn't import dot_parser, loading of dot files will not be possible, that doesn't solve the problem in Python 3.

Community
  • 1
  • 1
pterodragon
  • 429
  • 8
  • 16
  • Does this https://groups.google.com/forum/#!topic/pygraphviz-discuss/mbK5voZ9-hs explain some of the issue? – Joel Aug 24 '15 at 22:21
  • I think pygraphviz depends on parsing of the [DOT language](https://en.wikipedia.org/wiki/DOT_%28graph_description_language%29) which is the job of pydot? And now somehow it is pydot that is incompatible with Python3 rather than pygraphviz. – pterodragon Aug 25 '15 at 00:57
  • FWIW, I achieved the desired result by outputting a .dot file using `networkx.write_dot(G, 'graph.dot')` and then do the appropriate graphviz output command like `neato -T png graph.dot > graph.png`. (graphviz has to be installed beforehand; see the man page for more) – pterodragon Sep 01 '15 at 13:02
  • You might add that as an answer. Others with the same problem may not check the comments. – Joel Sep 02 '15 at 04:14
  • Some additional information: pygraphviz now claims to work with Python 3 right now but as of this moment I install it using the source by running `python3 setup.py install`, and `import pygraphviz` in Python3 gives me `ImportError: No module named '_graphviz'`. – pterodragon Sep 02 '15 at 15:31
  • Also the ubuntu package here does not work as well: http://packages.ubuntu.com/wily/python3-pygraphviz-dbg – pterodragon Sep 02 '15 at 15:35

3 Answers3

2

You can fix this by editing the line #292 from:

    pydot_node = pydot.Node(make_str(n)).get_name().encode('utf-8')

to remove the encode at the end:

    pydot_node = pydot.Node(make_str(n)).get_name() #.encode('utf-8')

I've reported this bug/fix here.

mfitzp
  • 15,275
  • 7
  • 50
  • 70
2

This seems to be the same issue as that the pydot you're using is a version incompatible with Python 3, which uses file(...). file(...) is removed in Python 3 already.

I noticed this issue and setup a Python 3 compatible version on PyPi.

For Linux systems for Python 3.x, try:

pip3 install pydot3

Or in general for Python 2.x, try:

pip install pydot3

Log0
  • 145
  • 1
  • 8
1

Not a very great answer but it acts as a workaround.

First output the .dot file by networkx.write_dot(G, 'graph.dot') using Python

and then do the appropriate graphviz output command like neato -T png graph.dot > graph.png on the command line.

pterodragon
  • 429
  • 8
  • 16