Since I spend a lot of time to solve this problem, so make a rocord for future reuse.
Environment
- Mao os 12.4
- Macbook air(M1,2020)
- PyCharm 2022.1.4 (Community Edition)
- Runtime version: 11.0.15+10-b2043.56 aarch64
- graphviz: stable 5.0.0
- pygraphviz: 1.9
Problem
- The problem is encounter after install
graphviz
- as to install pygraphviz you need
- Python (version 3.8, 3.9, or 3.10)
- Graphviz (version 2.42 or later)
- C/C++ Compiler
- as to install pygraphviz you need
- if you use normal command
pip install graphviz
to installgraphviz
first- then use
pip install pygraphviz
to installpygraphviz
- you would encounter a fatal error
- then use
fatal error: 'graphviz/cgraph.h' file not found
- the file cgraph.h cannot be found
- even you may find the file in graphviz package file
Why this problem happen
- the installation wizard of pygraphviz cannot find cgraph.h in his default searching path
- So, you need to provide searching path when install pygraphviz
Solution that work for me
Step 1. use Homebrew to graphviz
package first
- Install Homebrew from its official website
- type following command in Pycharm terminal
brew install graphviz
- it takes a long time to install
- then you should be able to find graphviz file in the following path :(/opt/homebrew/Cellar/graphviz)
Some explanation
- Homebrew is a kind of package manager for MacOS (or Linux)
- and all package installed via Homebrew is stored its Homebrew file which is under an opt file
- the opt file is a directory for installing unbundled packages
- i.e. Homebrew in this case help you to install graphviz and provide an searching path for further installation for pygraphviz
Step 2. Provide searching path when install pygraphviz
- type following command in Pycharm terminal
pip3 install --global-option=build_ext --global-option="-I$(brew --prefix graphviz)/include" --global-option="-L$(brew --prefix graphviz)/lib" pygraphviz
- pip3 is just to make sure package is install for python 3
- the
-I
and-L
flags tell the linker where to look for the required Graphviz libraries at build time and run time - --prefix will let you specific where the directory to go for
Top comments (1)
Hi @mathewchan , please remove the #help tag if you don't need help to solve it.
It's better to add #pygraphviz #Pycharm #MacOs and #python as tags so other users can find this quickly.
Thank you! 😁