45

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,262
  • 3
  • 15
  • 33

6 Answers6

129

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
  • 1,907
  • 15
  • 23
Sidharth Shah
  • 1,469
  • 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
19

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/" 
Alp Celik
  • 231
  • 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` – haku 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
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
  • 1,907
  • 15
  • 23
  • 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
13

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
  • 423
  • 1
  • 3
  • 12
Bart Theeten
  • 261
  • 3
  • 5
  • Worked for me in combination with pydot2 installation + but replace in above instructions 'urs' by 'usr' – Davy Oct 12 '16 at 18:12
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/
Alp Celik
  • 231
  • 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
  • 2,912
  • 2
  • 24
  • 37