0

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()
print(a._get_prog('fdp'))

g = qt.generate_random_graph(200, seed=3)
q = qt.QueueNetwork(g, seed=3)
q.max_agents = 20000
q.initialize(100)
q.simulate(10000)

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
    A.layout(prog=prog,args=args)
  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/bin/fdp
/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!

2
  • 1
    Please look at Aric's answer. Commented Oct 27, 2016 at 15:21
  • Also, this error will only occur if the code produces a runtime warning. I solved the cause of the runtime warning. Now my script completes without the above error. Commented Feb 5, 2018 at 14:03

1 Answer 1

3

There was a bug in the bytes/string handling of errors from graphviz that showed up with Python 3. The fix has been included (https://github.com/pygraphviz/pygraphviz/pull/104) in the pygraphviz github master branch. You can download that from https://github.com/pygraphviz/pygraphviz until we make a new release.

n.b. pygraphviz works correctly with Python3.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.