How to run neato from pygraphviz on Windows

Amlanza picture Amlanza · Feb 4, 2013 · Viewed 12k times · Source

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!

Answer

Antimony picture Antimony · Jun 2, 2013

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.