Search code examples
python-3.xanacondanetworkxgraphvizpygraphviz

Networkx issue : need to install Graphviz / Pygraphviz (Anaconda 2021.11, WIN10x64)


I'm using Jupyter Notebook to do graph analysis. I need to use Networkx to generate MultiDiGraph, I need to plot it as a tree, but there is an error occurring :

from networkx.drawing.nx_agraph import write_dot, graphviz_layout
write_dot(G,'test.dot')
plt.title('draw_networkx')
pos =graphviz_layout(G, prog='dot')
nx.draw(G, pos, with_labels=False, arrows=True)

ModuleNotFoundError                       Traceback (most recent call last)
D:\Programmes\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py in to_agraph(N)
    131     try:
--> 132         import pygraphviz
    133     except ImportError as e:

ModuleNotFoundError: No module named 'pygraphviz'

The above exception was the direct cause of the following exception:

ImportError                               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_33384/3939344011.py in <module>
----> 1 write_dot(G,'test.dot')
      2 plt.title('draw_networkx')
      3 pos =graphviz_layout(G, prog='dot')
      4 nx.draw(G, pos, with_labels=False, arrows=True)

D:\Programmes\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py in write_dot(G, path)
    183        Filename or file handle to write
    184     """
--> 185     A = to_agraph(G)
    186     A.write(path)
    187     A.clear()

D:\Programmes\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py in to_agraph(N)
    132         import pygraphviz
    133     except ImportError as e:
--> 134         raise ImportError("requires pygraphviz " "http://pygraphviz.github.io/") from e
    135     directed = N.is_directed()
    136     strict = nx.number_of_selfloops(N) == 0 and not N.is_multigraph()

ImportError: requires pygraphviz http://pygraphviz.github.io/

So I followed the instruction in this question, but I still get the same error. Maybe I need to do something else to add Pygraphviz to my Anaconda environment, so I could use networkx pygraphviz layouts.

Any advice on the way to do it properly ?

I have Graphviz 2.5 binaries, the Environment Variables Path is filled with the path of the bin, so I now can do the example showed in the question linked above. But the code I'm interested in necessitate Pygraphviz, so I feel like I'm step 1/N on my way to master my network.


Solution

  • You have installed the wrong graphviz wrapper, i.e. python-graphviz, but you need pyGraphviz:

    conda install -c conda-forge pygraphviz