Instruction to install pygraphviz package in Pycharm in MacOs

mathewchan

HHMathewChan

Posted on July 22, 2022

Instruction to install pygraphviz package in Pycharm in MacOs

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
  • if you use normal command pip install graphviz to install graphviz first
    • then use pip install pygraphviz to install pygraphviz
    • you would encounter a fatal error
fatal error: 'graphviz/cgraph.h' file not found
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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

Credit

💖 💪 🙅 🚩
mathewchan
HHMathewChan

Posted on July 22, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related