59

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.

15
  • 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? Commented Mar 27, 2013 at 14:33
  • I dont know if i have used virtulenv. How to check if i have installed pyhraphviz and python on the same environment? Commented Mar 27, 2013 at 14:36
  • 1
    I just typed easy_install pyhraphviz Commented Mar 27, 2013 at 14:37
  • 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. Commented Mar 27, 2013 at 14:40
  • 3
    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' Commented Mar 27, 2013 at 15:51

12 Answers 12

141

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
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, pkg-config is important, otherwise pip install pygraphviz will can not be compiled, cause library_path.
ImportError: undefined symbol: Agundirected , as described here.
make sure you restart your terminal...so your changes gets affected
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/" 

4 Comments

Thanks for this, also works for me on Ubuntu 16.04 LTS when trying to install pygraphz via pip
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”.
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
also works in ubuntu 20.04 LTS. I needed this in order to install it ..
17

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"

3 Comments

Thank you for this, was the only solution that worked. Just needed to change GRAPHVIZ_DIR to my local HomeBrew path
With more recent versions: export GRAPHVIZ_DIR=/opt/homebrew/Cellar/graphviz/7.1.0/
For me it worked to use the command from the note from the docs: 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
16

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

3 Comments

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?
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.
Thanks! For me just installing python-pygraphviz fixed the problem
16

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

2 Comments

Worked for me in combination with pydot2 installation + but replace in above instructions 'urs' by 'usr'
How would this work in a virtual environment?
9

This is the only command that worked for me on my M1 MacBook Pro (MacOS 14.2.1):

python3 -m pip install -U --no-cache-dir  \
        --config-settings="--global-option=build_ext" \
        --config-settings="--global-option=-I$(brew --prefix graphviz)/include/" \
        --config-settings="--global-option=-L$(brew --prefix graphviz)/lib/" \
        pygraphviz

Credit to this guy here

1 Comment

YOU ARE A GOD SEND!
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/

Comments

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.

Comments

2

For those using poetry this might help

CFLAGS="-I$(brew --prefix graphviz)/include/ -L$(brew --prefix graphviz)/lib/" poetry install

Comments

0

check the answer here: Installing pygrahviz in google colab

graphviz is installed already, but need lib too

!apt install libgraphviz-dev !pip install pygraphviz

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

Thanks to krisgeus for macos/brew/poetry. More detailed:

brew install graphviz
CFLAGS="-I$(brew --prefix graphviz)/include/ -L$(brew --prefix graphviz)/lib/" poetry add pygraphviz

Comments

-1

In Colab,

!apt  install  libgraphviz - dev
!pip install pygraphviz

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.