How to run neato from pygraphviz on Windows

柔情痞子 提交于 2020-01-01 02:45:12

问题


I am trying to use pygraphviz and networkx in python (v 2.7) to create a network map. I found a script that looks very useful on stackoverflow:

import networkx as nx
import numpy as np
import string
import pygraphviz

dt = [('len', float)]
A = np.array([(0, 0.3, 0.4, 0.7),
               (0.3, 0, 0.9, 0.2),
               (0.4, 0.9, 0, 0.1),
               (0.7, 0.2, 0.1, 0)
               ])*10
A = A.view(dt)

G = nx.from_numpy_matrix(A)
G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())),string.ascii_uppercase)))    

G = nx.to_agraph(G)

G.node_attr.update(color="red", style="filled")
G.edge_attr.update(color="blue", width="2.0")

G.draw('/tmp/out.png', format='png', prog='neato')

I get an error on the last line, basically it cannot find neato:

"ValueError: Program neato not found in path."

The error refers to the agraph.py file for pygraphviz, but I cannot see anything that could be causing the problem when I look through agraph.py

Any ideas how to resolve this? I am using windows and IDLE for my coding. Thanks!


回答1:


I had the same problem. Here's what I did in case anyone else is struggling to get pygraphvis working on Windows.

First off, I installed graphviz. I tried to install pygraphvis thrugh pip, but it refused to work. Eventually, I found the unofficial Windows binaries, so I installed that. Importing the module now works, but calling G.layout() led to the above error.

Calling neato -V worked, so it was on my PATH. I figured out that the problem was that python was running in a command prompt that was created prior to installing pygraphvis, so PATH wasn't updated. Restarting the command prompt fixed this, but led to a new error, something about C:\Program not being a valid command.

I figured that pygraphvis was probably failing to quote the path correctly, meaning it cuts off at the space in Program Files. I solved the problem by symlinking it to a path without spaces.

mklink /d C:\ProgramFilesx86 "C:\Program Files (x86)"

Note that this must be run in admin mode. You can do it by going to the start menu, typing in cmd, and then hitting Ctrl+shift+enter.

After this, I edited my PATH to refer to the symlink, restarted cmd, and everything worked.




回答2:


The problem is that pygraphviz call an external program, a part of the graphviz suite called neato, to draw the graph. What is happening is that you doesn't have graphviz installed and when python try to call it it complains about not finding it. Actually pygraphviz is just a wrapper that gives you the possibility to call graphviz from inside python, but per se doesn't do anything and doesn't install graphviz by default.

The easiest solution is to try a different solution for the plot instead of neato. the accepted option are:

neato
dot
twopi
circo
fdp
nop

try one of those and see if one of them works. Otherwise you can install graphviz, that will give you the required program. It's and open-source program available on every platform, so it shouldn't be a problem to install it.

see at http://www.graphviz.org/

If you simply need to have a sketch of the graph you can use the networkx.draw function on a networkx graph, that uses matplotlib to create an interactive plot.

import networkx as nx
G = G=nx.from_numpy_matrix(A)
nx.draw(G)



回答3:


Your problem is that neato is missing.
neato is part of the graphviz suite which you can install on your PC e.g. from here. (I used the .msi)

Now neato is "installed", but your system does not know where. So add the directory where neato.exe is contained in to your PATH environment variable. On Win10, this can be done with Start -> Edit environment variables for your account -> select path in the upper window -> edit -> New -> C:\Program Files (x86)\Graphviz2.38\bin\ or whatever your install directory is.




回答4:


There is probably more than one cause for this error, but if it is caused by a missing path to the graphviz modules [neato,dot,twopi,circo,fdp,nop], then there is one hack that worked for me. I'm currently asking what the correct solution is, but you can use this

if  not 'C:\\Program Files (x86)\\Graphviz2.38\\bin' in os.environ["PATH"]: 
    os.environ["PATH"] += os.pathsep + 'C:\\Program Files (x86)\\Graphviz2.38\\bin' 

at the beginning of your script. To generalize, if your graphviz files are saved somewhere else:

graph_path='your_bin_folder_path'
    if  not graph_path in os.environ["PATH"]: 
        os.environ["PATH"] += os.pathsep + graph_path

In particular, this worked on windows 10, using anaconda navigator and python version 3.7.



来源:https://stackoverflow.com/questions/14679434/how-to-run-neato-from-pygraphviz-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!