Unis Unis - 1 year ago 83
Python Question

Pygraphviz/Python3/Mac: TypeError

I have been trying to get Pygraphviz to work with Python3 on Mac, and I am unable to solve a TypeError.

Specifically, the output of the following code (Python3/Eclipse Neon/macOS 10.10.5, adapted tutorial from here)

import networkx as nx
import queueing_tool as qt
import pygraphviz as pgv

a = pgv.AGraph()

g = qt.generate_random_graph(200, seed=3)
q = qt.QueueNetwork(g, seed=3)
q.max_agents = 20000

pos = nx.nx_agraph.graphviz_layout(g.to_undirected(), prog='neato')

is the following error:

/usr/local/bin/fdp //<-- this shows that Eclipse finds 'fdp' after adding /usr/local/bin to $PATH, ERROR in the following:
Traceback (most recent call last):
File "/Users/flurin/Documents/workspace/pedQN/main.py", line 14, in <module>
pos = nx.nx_agraph.graphviz_layout(g.to_undirected(), prog='neato')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/networkx/drawing/nx_agraph.py", line 228, in graphviz_layout
return pygraphviz_layout(G,prog=prog,root=root,args=args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/networkx/drawing/nx_agraph.py", line 262, in pygraphviz_layout
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pygraphviz/agraph.py", line 1358, in layout
data = self._run_prog(prog, ' '.join([args, "-T", fmt]))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pygraphviz/agraph.py", line 1338, in _run_prog
warnings.warn(b"".join(errors), RuntimeWarning)
TypeError: cannot use a string pattern on a bytes-like object

If I run the same code using Python 2.7.x (requiring only changing the 'print(arg)' to 'print arg') I get the following output (tons of warnings, but it works):

/usr/local/lib/python2.7/site-packages/pygraphviz/agraph.py:1338: RuntimeWarning: Warning: node 0, position [ 5.50797903 7.08147823], expected two floats
Warning: node 12, position [ 5.44649018 7.80314765], expected two floats
[... here other warnings of the same kind]
Warning: Overlap value "prism" unsupported - ignored

warnings.warn(b"".join(errors), RuntimeWarning)

So my question is, how can I get pygraphviz to work with Python3 as well? I have no idea where to look. Note that there is no difference in the errors I get when running the above code from the terminal or from Eclipse.

Is there an incompatibility with Python3? I remember having been told a few years ago that for "scientific purposes" it may still be better to use Python2.x. Is this still the case?

Thanks a lot for your help in advance!

Answer Source

As the documentation notes:

To use PyGraphviz you need Python version 2.6.x or 2.7.x. PyGraphviz does not work with Python 3