51

I have installed pygraphviz using easy_install But when i launch python i have an error:

>>>import pygraphviz as pgv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pygraphviz
>>> 

Using Ubuntu 12.04 and gnome-terminal.

Sashko Lykhenko
  • 1,554
  • 4
  • 20
  • 36

8 Answers8

137

Assuming that you're on Ubuntu please look at following steps

  1. sudo apt-get install graphviz libgraphviz-dev pkg-config
  2. Create and activate virtualenv if needed. The commands looks something like sudo apt-get install python-pip python-virtualenv
  3. Run pip install pygraphviz
  4. Run terminal and check by importing and see if it works
Sean
  • 2,315
  • 20
  • 25
Sidharth Shah
  • 1,569
  • 1
  • 11
  • 14
  • 2
    Yes, `pkg-config` is important, otherwise `pip install pygraphviz` will can not be compiled, cause `library_path`. – Honghe.Wu Oct 30 '13 at 02:27
  • 4
    `ImportError: undefined symbol: Agundirected `, as described [here](http://stackoverflow.com/questions/32885486/import-pygraphviz-not-working-ubuntu). – SparkAndShine Oct 02 '15 at 14:29
  • make sure you restart your terminal...so your changes gets affected – sumitkanoje Feb 17 '21 at 11:58
20

On Ubuntu 14.04, there is a problem in auto detecting graphviz library and include files. If you follow the steps below probably you'll be safe.

1) sudo apt-get install graphviz libgraphviz-dev pkg-config python-pip
2) pip install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/" 
celikalp
  • 241
  • 2
  • 2
  • 1
    Thanks for this, also works for me on Ubuntu 16.04 LTS when trying to install pygraphz via pip – rwenz3l Jun 14 '17 at 13:08
  • These install options also worked for me experiencing the same error on macOS (having run `brew install graphviz`), except the path was `/usr/local/include/graphviz` and the lib was `/usr/local/lib/graphviz`, so full command was: `pip3.6 install pygraphviz --install-option="--include-path=/usr/local/include/graphviz/" --install-option="--library-path=/usr/local/lib/graphviz”`. – Chris Jun 25 '18 at 15:07
  • On Ubuntu 18.04, I could pull it off with just `sudo apt-get install graphviz libgraphviz-dev graphviz-dev` followed by `pip install pygraphviz` – h2ku Feb 13 '19 at 23:59
  • also works in ubuntu 20.04 LTS. I needed this in order to install it .. – Lucas Aimaretto Aug 13 '20 at 20:19
15

On Mac OSX, the following did the trick for me:

pip install graphviz
pip install cgraph
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig 
cd /usr/local/include/graphviz 
sudo ln -s . graphviz 
pip install pygraphviz

[As suggested, fixed typo from previously /urs/local/ to /usr/local/]

Unis
  • 614
  • 1
  • 5
  • 17
Bart Theeten
  • 551
  • 6
  • 6
14

The quick and easy solution is:

sudo apt-get install -y python-pygraphviz

using pip will also work, but make sure you have graphviz, libgraphviz-dev, and pkg-config already installed.

sudo apt-get install -y graphviz libgraphviz-dev pkg-config python-pip
sudo pip install pygraphviz
Sean
  • 2,315
  • 20
  • 25
  • Why are these needed? Installing (and using) python-pygraphviz seemed to work fine for me without libgraphviz-dev. Why then is this needed in advance if its not an explicit dependency, and why the others? – CPBL Mar 20 '16 at 20:08
  • this was answered almost 3 years ago, its possible that they've changed how pygraphviz is packaged by now so that this is not all necessary. If that's the case, you should post an answer here to that effect. – Sean Mar 22 '16 at 16:48
  • Thanks! For me just installing `python-pygraphviz` fixed the problem – Bretsko Apr 28 '17 at 01:46
14

I use mac m1, I fix this by this.

#install graphviz first
brew install graphviz

#check your graphviz path   
brew info graphviz

#change to your dir
export GRAPHVIZ_DIR="/usr/local/Cellar/graphviz/<VERSION>" #3.0.0 in my case

#finally run this 
pip install pygraphviz --global-option=build_ext --global-option="-I$GRAPHVIZ_DIR/include" --global-option="-L$GRAPHVIZ_DIR/lib"
Yicheng Wang
  • 141
  • 1
  • 3
  • Thank you for this, was the only solution that worked. Just needed to change `GRAPHVIZ_DIR` to my local HomeBrew path – alxlives Apr 06 '22 at 14:19
  • 2
    With more recent versions: export GRAPHVIZ_DIR=/opt/homebrew/Cellar/graphviz/7.1.0/ – allprog Feb 06 '23 at 17:29
  • For me it worked to use the command from the note from the [docs](https://pygraphviz.github.io/documentation/stable/install.html#homebrew): `pip install --use-pep517 \ --config-setting="--global-option=build_ext" \ --config-setting="--build-option=-I$(brew --prefix graphviz)/include/" \ --config-setting="--build-option=-L$(brew --prefix graphviz)/lib/" \ pygraphviz` – Hans Bambel Aug 30 '23 at 09:43
4

On Mac OSX El Capitan, Bart Theeten's solution works but there are two things you need to be careful. Initially, make sure that you installed graphviz on your computer. You can use homebrew:

brew install graphviz

Other thing is to make sure you add the path of packages to PYTHONPATH

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages/
celikalp
  • 241
  • 2
  • 2
3

Under Ubuntu 15.10+ (ie 2015ish Debian), the quick and easy solution is:

sudo apt-get install python-pygraphviz

Any dependencies are properly pulled by apt.

CPBL
  • 3,783
  • 4
  • 34
  • 44
0

In Colab,

!apt  install  libgraphviz - dev
!pip install pygraphviz

Credits: https://gist.github.com/korakot/a80c04a1945b06e2f4a053f92fecfbf9

Lin
  • 11
  • 3