How to run neato from pygraphviz on Windows
Asked Answered
G

5

11

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!

Gallnut answered 4/2, 2013 at 1:50 Comment(1)
I have that error in WSL 2 as well.Cofferdam
S
13

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.

Sleeve answered 2/6, 2013 at 19:9 Comment(1)
For clarity, it is now difficult to access the "path" in windows10. To access it, right click on the start button (bottom left), then click on "search", then enter "control pannel", then click on "system" on the right part of the window, then on the left part of the window click on "advanced system parameter". After changing path, remember to close and open again python and command prompt for an update of the changed path.Whensoever
S
4

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)
Seedling answered 4/2, 2013 at 20:31 Comment(3)
First, thanks for responding. I do have graphviz installed. Now, when I run my script I get this error: Traceback (most recent call last): File "C:/Python27/2_5.py", line 22, in <module> G.draw('/tmp/out.png', format='png', prog='neato') File "C:\Python27\lib\site-packages\pygraphviz\agraph.py", line 1422, in draw fh=self._get_fh(path,'w+b') File "C:\Python27\lib\site-packages\pygraphviz\agraph.py", line 1458, in _get_fh fh = file(path,mode=mode) IOError: [Errno 2] No such file or directory: '/tmp/out.png'Gallnut
It has some problem creating the file /tmp/out.png. This may be due to the operating system. /tmp is a normal directory in all linux distribution, so it will just need to create tha file. If you are working on windows that directory will probably not be present, hence the error (you are trying to create the file in a directory that doesn't exists). If you replace it with just "out.png" without specifying the directory it should create it in the current directory without complaining. Let me know if this does the trick!Seedling
Now, it would be nice to also know how to use those other options.Cofferdam
A
2

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.

Antimony answered 15/10, 2018 at 7:38 Comment(1)
This answer is really good generally speaking. The basic idea was right for me, but for some reason adding that path to the Env. Vars. didn't work. I'll post my solution as well.Glynisglynn
G
2

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.

Glynisglynn answered 4/11, 2019 at 20:57 Comment(3)
Since you state that my answer did not work for you: Did you close and reopen the terminal after setting the path variable like I described? Because that's something one needs to do, but if you did that, I don't know eitherAntimony
Oooh, that may have been all I needed.Glynisglynn
@bartcubrich I couldn't thank you enough. Your solution did the trick for me. The correct solution should be whatever works.Portly
B
1

Try something like this to see where pygraphviz thinks your external programs are:

# Get paths of graphviz programs
import pygraphviz as pgv

A = pgv.AGraph()
progs_list = ['neato', 'dot', 'twopi', 'circo', 'fdp', 'nop', 'wc', 'acyclic', 'gvpr',
              'gvcolor', 'ccomps', 'sccmap', 'tred', 'sfdp', 'unflatten']
for prog in progs_list:
    try:
        runprog = A._get_prog(prog)
        print(f'{runprog}')
    except ValueError as e:
        print(f'{prog} gets this error: {str(e).strip()}')

After looking at the results, it's a lot of work outside your IDE installing Graphviz and setting up your Path environmental variable in the System control panel, etc.

Brahmin answered 9/2, 2021 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.