Python does not see pygraphviz

62,261

Solution 1

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

Solution 2

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/" 

Solution 3

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

Solution 4

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/]

Solution 5

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/
Share:
62,261

Related videos on Youtube

Sashko Lykhenko
Author by

Sashko Lykhenko

BY DAY: PhD student at Institute of molecular biology and genetics Kyiv, Ukraine

Updated on March 30, 2022

Comments

  • Sashko Lykhenko
    Sashko Lykhenko about 2 years

    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.

    • Sidharth Shah
      Sidharth Shah over 11 years
      Have you used virtulenv, if so can you please confirm if the environment on which you installed the package is the same from which you may be calling script or invoking shell?
    • Sashko Lykhenko
      Sashko Lykhenko over 11 years
      I dont know if i have used virtulenv. How to check if i have installed pyhraphviz and python on the same environment?
    • Sashko Lykhenko
      Sashko Lykhenko over 11 years
      I just typed easy_install pyhraphviz
    • Sidharth Shah
      Sidharth Shah over 11 years
      Ok do the following in your gnoe-terminal 1. pip install virtualenv 2. virtualenv myenv 3. source myenv/bin/activate 4. pip install pygraphviz Then run python and try import pygraphviz as check if it works.
    • Sidharth Shah
      Sidharth Shah over 11 years
      Also take a look at this nice Tutorial on pip and virtualenv when you have time.
    • Sashko Lykhenko
      Sashko Lykhenko over 11 years
      I dont have pip installed. apt-get does not contain it either. And apt-get does not contain virtualenv
    • Sidharth Shah
      Sidharth Shah over 11 years
      Try sudo apt-get install python-pip python-virtualenv
    • Sashko Lykhenko
      Sashko Lykhenko over 11 years
      After "pip install pygraphviz": Command python setup.py egg_info failed with error code 1 in /home/sashko/src/webkit2/webkit/Tools/Scripts/myenv/build/py‌​graphviz Storing complete log in /home/sashko/.pip/pip.log
    • Sashko Lykhenko
      Sashko Lykhenko over 11 years
      OSError: Error locating graphviz.
    • Sidharth Shah
      Sidharth Shah over 11 years
      Just curios did you do sudo apt-get install graphviz before you're doing pip install pygraphviz. In my case I was missing that, on my local VM.
    • Sashko Lykhenko
      Sashko Lykhenko over 11 years
      yes, i did sudo apt-get install graphviz before doing pip install pygraphviz.
    • Sashko Lykhenko
      Sashko Lykhenko over 11 years
      How to find out where "graphviz" was installed? And were it should be installed so that pyhraphviz setup.py could find it?
    • Sidharth Shah
      Sidharth Shah over 11 years
      Ok I figure the issue. You might be missing libgraphviz-dev. You can install it using sudo apt-get install libgraphviz-dev. You can check if graphviz is installed using dpkg -l | grep 'graphviz'
    • Sashko Lykhenko
      Sashko Lykhenko over 11 years
      Worked! Will it work only in myenv?
    • Sidharth Shah
      Sidharth Shah over 11 years
      Yes it would. Give it a try.
  • Honghe.Wu
    Honghe.Wu over 10 years
    Yes, pkg-config is important, otherwise pip install pygraphviz will can not be compiled, cause library_path.
  • SparkAndShine
    SparkAndShine over 8 years
    ImportError: undefined symbol: Agundirected , as described here.
  • CPBL
    CPBL over 8 years
    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?
  • Sean
    Sean over 8 years
    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.
  • Davy
    Davy over 7 years
    Worked for me in combination with pydot2 installation + but replace in above instructions 'urs' by 'usr'
  • Bretsko
    Bretsko about 7 years
    Thanks! For me just installing python-pygraphviz fixed the problem
  • rwenz3l
    rwenz3l about 7 years
    Thanks for this, also works for me on Ubuntu 16.04 LTS when trying to install pygraphz via pip
  • Chris
    Chris about 6 years
    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”.
  • haku
    haku over 5 years
    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
  • Lucas Aimaretto
    Lucas Aimaretto almost 4 years
    also works in ubuntu 20.04 LTS. I needed this in order to install it ..
  • sumitkanoje
    sumitkanoje over 3 years
    make sure you restart your terminal...so your changes gets affected
  • alxlives
    alxlives about 2 years
    Thank you for this, was the only solution that worked. Just needed to change GRAPHVIZ_DIR to my local HomeBrew path