I am trying to install pygraphviz on Windows 10. There are many solutions to this problem online, but none have yet worked for me. The precise problem I'm having is with this via jupyter notebook-->
[1] import networkx as nx
import pylab as plt
from networkx.drawing.nx_agraph import graphviz_layout
[2]G = nx.DiGraph()
G.add_node(1,level=1)
G.add_node(2,level=2)
G.add_node(3,level=2)
G.add_node(4,level=3)
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(2,4)
nx.draw(G, pos=graphviz_layout(G), node_size=1600, cmap=plt.cm.Blues,
node_color=range(len(G)),
prog='dot')
plt.show()
I get the following errors after [2]:
ModuleNotFoundError Traceback (most recent call last)
C:\Users\name\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py
in
pygraphviz_layout(G, prog, root, args)
254 try:
--> 255 import pygraphviz
256 except ImportError:
ModuleNotFoundError: No module named 'pygraphviz'
and
ImportError Traceback (most recent call last)
<ipython-input-2-86a15892f0f0> in <module>()
9 G.add_edge(2,4)
10
---> 11 nx.draw(G, pos=graphviz_layout(G), node_size=1600, cmap=plt.cm.Blues,
12 node_color=range(len(G)),
13 prog='dot')
C:\Users\name\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py in graphviz_layout(G, prog, root, args)
226
227 """
--> 228 return pygraphviz_layout(G,prog=prog,root=root,args=args)
229
230 def pygraphviz_layout(G,prog='neato',root=None, args=''):
C:\Users\name\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py in pygraphviz_layout(G, prog, root, args)
256 except ImportError:
257 raise ImportError('requires pygraphviz ',
--> 258 'http://pygraphviz.github.io/')
259 if root is not None:
260 args+="-Groot=%s"%root
ImportError: ('requires pygraphviz ', 'http://pygraphviz.github.io/')
Here's what I've tried to resolve this
(1) Regular pip install: "pip install pygraphviz" This is the error I get at the end. EDIT I get the same error even if I run cmd as admin.
Command "C:\Users\name\Anaconda3\python.exe -u -c "import setuptools,
tokenize;__file__='C:\\Users\\name~1\\AppData\\Local\\Temp\\pip-build-
n81lykqs\\pygraphviz\\setup.py';f=getattr(tokenize, 'open', open)
(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code,
__file__, 'exec'))" install --record C:\Users\name~1\AppData\Local\Temp\pip-
b3jz1lk5-record\install-record.txt --single-version-externally-managed --
compile" failed with error code 1 in C:\Users\name~1\AppData\Local\Temp\pip-
build-n81lykqs\pygraphviz\
(2) Downloading and installing graphviz-2.38.msi, and then downloading both the 64-bit versions of the wheel. This is the result.
C:\Users\name\Anaconda3>pip install pygraphviz-1.3.1-cp34-none-
win_amd64.whl
pygraphviz-1.3.1-cp34-none-win_amd64.whl is not a supported wheel on this
platform.
C:\Users\name\Anaconda3>pip install pygraphviz-1.3.1-cp27-none-
win_amd64.whl
pygraphviz-1.3.1-cp27-none-win_amd64.whl is not a supported wheel on this
platform.
What I would like to try, but am not sure how to do properly:
Edit setup.py. I have read a lot about people finding solutions in changing the paths, but I'm not really sure how to do this. This method looks very complex.
Thank you for any help/insight!
Solved it on Windows 10 64-bits and Python 3.6.
Steps:
C:\Program Files (x86)\Graphviz2.38\bin
in your Windows path.Install the graphviz wheel.
pip install graphviz-0.8.3-py2.py3-none-any.whl
Done!
The most voted answers seemed to be installing graphviz, rather than pygraphviz.
If you are using a conda environment, you may try using this channel:
conda install graphviz pygraphviz -c alubbock
I tried it out with
networkx 2.1
, it worked fine.Updated the repo: [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyGraphviz/v1.5/Graphviz-2.42.2 (as URL states, used official Graphviz version 2.42.2).
Added .whls (win_amd64, win32) for Python:
For Python 2.7, they are already built: [UCI.LFD]: Unofficial Windows Binaries for Python Extension Packages - PyGraphviz, an interface to the Graphviz graph layout and visualization package..
Notes:
Anyone who wants to know more details about the build process, read on!
1. Intro
Almost 2 years later, and the problem (well, not exactly as in the question) still persists.
I want to start by emphasizing the difference between the 2 packages:
In Anaconda environment, [SO]: Installing PyGraphviz on Windows 10 64-bit, Python 3.6 (@TomHanks's answer) works perfectly.
pygraphwiz only has available for download an archive (.zip, in this case) file, meaning that it contains (C / C++) sources.
A couple of words about packages (.whls) whose names contain things like cp34-none-win_amd64 (check [SO]: What does version name 'cp27' or 'cp35' mean in Python? (@WayneWerner's answer) for details):
Now, many packages have prebuilt binaries for most common Python versions running on various OSes (e.g. [PyPI]: mysql-connector-python - Download files), but just as many don't, and those only contain sources. Unfortunately, PyGraphviz is in the 2nd category. For the latter ones,
pip install
will:As a side note:
pip -v ...
enables verbose mode for the current command, which comes in extremely handy when experiencing install errors.Back to our problem: Python 3.6 needs VStudio 2015 ([Python.Wiki]: WindowsCompilers).
This is a very vast topic, I covered some parts in:
You should check them before proceeding and also keep them open, as you will definitely need them in the next steps.
I have VStudio 2015 Community (among many other versions) installed, you should install it too, it's free ([MS.VStudio]: Still want an older version?).
PyGraphviz depends on [Graphviz]: Graph Visualization Software. So, at build time it will need (parts of) Graphviz (which also has other dependencies of its own) to be already built. Unfortunately, I couldn't find prebuilt binaries (there is [Graphviz]: Windows Packages - graphviz-2.38.zip, but that's not helping), so it will have to be built manually.
Before going further:
This is my top dir, any sources are downloaded in the src dir, binaries will be placed in the bin dir.
2. Build Graphviz
Before starting, I want to mention that I heavily rely on Cygwin (you don't have to), and some of my tools are installed there, so I'll be alternating between Cygwin and cmd terminals (which might be confusing).
[Graphviz]: Graphviz Build Instructions for Windows states:
First, we need to download everything:
You'll end up with a dir that contains ~320 MiB of stuff. The dir contains a graphviz.sln file, which is a VStudio (2015) solution file which contains 63 projects.
Looking at the Anaconda or Python 2.7 pygraphviz (built) package, it only depends on cgraph.dll, which in turn depends on cdt.dll, so only the 2 projects are relevant to us. Note that these 2 projects might not need all the git submodules (so the dir might be trimmed down), but I didn't investigate further.
Unfortunately, the projects are only configured for 32 bit (Win32 platform). The 64 bit one must be manually added (I did it from VStudio IDE - and also described the process in one of my answers that I referenced). After saving the projects, they will be shown as modified by git:
The 3rd item is because I needed to reset some security permissions on 2 executables (used when building cgraph):
which were not set properly (most likely, because of Cygwin).
You can build the 2 projects from IDE, but I chose command line ([Ms.Docs]: MSBuild command-line reference) since I find it more flexible:
So, we have everything needed (2 .lib and 2 .dll files) in order to go on.
3. Build PyGraphviz
PyGraphviz sources are (downloaded from [GitHub]: pygraphviz/pygraphviz - (pygraphviz-1.5) pygraphviz-pygraphviz-1.5.zip and) unpacked in src/pygraphviz/pygraphviz-pygraphviz-1.5.
One more adjustment is needed to Graphviz (probably it's done as part of another project - an install step): preparing the header files:
Unfortunately, PyGraphviz does not build OOTB, because of [GitHub]: pygraphviz/pygraphviz - Python 3 support. To fix that, [GitHub]: eendebakpt/pygraphviz - Workaround for PyIOBase_Type for Python2 on win must be applied. I adapted it to work with the current sources (as it doesn't work OOTB, as well :X( ) for graphviz_wrap.cpp only:
pygraphviz-1.5-all-pyiobase_b85d12ac22d39063f7dbcc396e825c563431e352.patch:
That is a diff (patch). See [SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer) (Patching utrunner section) for how to apply patches on Win (basically, every line that starts with one "+" sign goes in, and every line that starts with one "-" sign goes out).
As seen, the module was successfully imported.
As a remark, the 2 .dll dependencies (from previous section) must be available when the module is imported, so their dir is added to %PATH%.
Of course this is only a (lame) workaround (gainarie), this shouldn't happen every time one has to work with the package.
I don't know (yet) how to instruct setup.py to also copy them in the package build / install dir, so as an alternative (also workaround) one has to manually copy them in pygraphviz install dir (next to _graphviz.cp36-win_amd64.pyd, which is (in my case): "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\lib\site-packages\pygraphviz").
4. Shortcut
Since the whole process is complex and requires lots of manual interventions and hacks, I've managed to build (with minor setup.py modifications) the (wheel) package.
I am not aware of a simple way to make it publicly available, so (although I know it's a bad practice,) I uploaded it at [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyGraphviz/v1.5/Graphviz-2.42.2/pygraphviz-1.5-cp36-cp36m-win_amd64.whl.
You can download it, then install it like (this is one way):
Note: It works for Anaconda environments as well!
Here's what worked for me:
Win 7 AMD64
C:\Program Files (x86)\Graphviz2.38\bin
to your PATH environment variable.conda create --name digraphs python=3.4 anaconda
.activate digraphs
.pip install pygraphviz-1.3.1-cp34-none-win_amd64.whl
.python ./gviz_simple.py
.deactivate
I put some stuff up on github about it. It's messy, use at your own risk: https://github.com/darkhipo/Easy-Digraph-Draw