Python does not see pygraphviz
Asked Answered
I

10

58

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.

Income answered 27/3, 2013 at 14:28 Comment(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?Responsiveness
I dont know if i have used virtulenv. How to check if i have installed pyhraphviz and python on the same environment?Income
I just typed easy_install pyhraphvizIncome
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.Responsiveness
Also take a look at this nice Tutorial on pip and virtualenv when you have time.Responsiveness
I dont have pip installed. apt-get does not contain it either. And apt-get does not contain virtualenvIncome
Try sudo apt-get install python-pip python-virtualenvResponsiveness
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/pygraphviz Storing complete log in /home/sashko/.pip/pip.logIncome
OSError: Error locating graphviz.Income
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.Responsiveness
yes, i did sudo apt-get install graphviz before doing pip install pygraphviz.Income
How to find out where "graphviz" was installed? And were it should be installed so that pyhraphviz setup.py could find it?Income
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'Responsiveness
Worked! Will it work only in myenv?Income
Yes it would. Give it a try.Responsiveness
R
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
Responsiveness answered 27/3, 2013 at 19:40 Comment(3)
Yes, pkg-config is important, otherwise pip install pygraphviz will can not be compiled, cause library_path.Cassiopeia
ImportError: undefined symbol: Agundirected , as described here.Scroop
make sure you restart your terminal...so your changes gets affectedTrichomoniasis
T
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/" 
Tweet answered 17/11, 2016 at 10:37 Comment(4)
Thanks for this, also works for me on Ubuntu 16.04 LTS when trying to install pygraphz via pipGoofball
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”.Halftruth
On Ubuntu 18.04, I could pull it off with just sudo apt-get install graphviz libgraphviz-dev graphviz-dev followed by pip install pygraphvizGolfer
also works in ubuntu 20.04 LTS. I needed this in order to install it ..Georganngeorge
R
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
Rem answered 3/7, 2013 at 19:35 Comment(3)
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?Burnette
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.Rem
Thanks! For me just installing python-pygraphviz fixed the problemAllina
P
16

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"
Pastime answered 29/3, 2022 at 12:12 Comment(3)
Thank you for this, was the only solution that worked. Just needed to change GRAPHVIZ_DIR to my local HomeBrew pathVelvavelvet
With more recent versions: export GRAPHVIZ_DIR=/opt/homebrew/Cellar/graphviz/7.1.0/Ocieock
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/" \ pygraphvizRadiancy
C
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/]

Cindacindee answered 11/10, 2016 at 11:35 Comment(2)
Worked for me in combination with pydot2 installation + but replace in above instructions 'urs' by 'usr'Allegedly
How would this work in a virtual environment?Gomez
V
7

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

Venesection answered 9/1 at 8:26 Comment(1)
YOU ARE A GOD SEND!Pascasia
T
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/
Tweet answered 15/11, 2016 at 4:41 Comment(0)
B
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.

Burnette answered 22/3, 2016 at 20:3 Comment(0)
P
0

In Colab,

!apt  install  libgraphviz - dev
!pip install pygraphviz

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

Portiere answered 11/2, 2022 at 12:12 Comment(0)
M
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

Marcello answered 2/7 at 9:18 Comment(1)
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.Arianism

© 2022 - 2024 — McMap. All rights reserved.