我一直试图让Pygraphviz在Mac上使用Python3,我无法解决TypeError。
具体来说,以下代码的输出(Python3 / Eclipse Neon / macOS 10.10.5,来自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')
是以下错误:
/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
如果我使用Python 2.7.x运行相同的代码(只需要更改&#39; print(arg)&#39;打印arg&#39;)我得到以下输出(吨警告,但它有效):
/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)
所以我的问题是,如何让pygraphviz与Python3一起使用?我不知道在哪里看。请注意,从终端或Eclipse运行上述代码时出现的错误没有区别。
是否与Python3不兼容?我记得几年前被告知为了科学目的&#34;使用Python2.x可能仍然更好。这仍然是这样吗?
非常感谢您的帮助!
答案 0 :(得分:3)
来自graphviz的错误的字节/字符串处理中出现了一个错误,该错误出现在Python 3中。修复已包含在pygraphviz github主分支中(https://github.com/pygraphviz/pygraphviz/pull/104)。您可以从https://github.com/pygraphviz/pygraphviz下载,直到我们发布新版本。
n.b。 pygraphviz与Python3一起正常工作。