Keras: "RuntimeError: Failed to import pydot." after installing graphviz and pydot
Question:
I’m using Anaconda Python 2.7 on windows 10
I was planning on doing Keras visualization so (whilst spyder was open) I opened the Anaconda command prompt and pip installed graphviz and pydot. Now when I try run the following:
from keras.models import Sequential
or any sort of “from keras.” , I get the error:
ImportError: cannot import name gof
I have uninstalled and reinstalled Keras, Graphviz and pydot. i am using the development version of theano. I cannot find a fix.
P.S
If I uninstall graphviz and pydot, keras works again
EDIT
After uninstalling anaconda and reinstalling it including theano, keras, graphviz and pydot I now get the following error:
from keras.utils.visualize_util import plot
Using Theano backend.
Using gpu device 0: GeForce GTX 970M (CNMeM is disabled, cuDNN not available)
Traceback (most recent call last):
File "<ipython-input-1-65016ddab3cd>", line 1, in <module>
from keras.utils.visualize_util import plot
File "C:Anaconda2libsite-packageskerasutilsvisualize_util.py", line 8, in <module>
raise RuntimeError('Failed to import pydot. You must install pydot'
RuntimeError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.
I used pip install graphviz
and pip install git+https://github.com/nlhepler/pydot.git
Answers:
The error message is a bit misleading, as you can see here. The problem is that graphviz is not installed.
But you mention that graphviz was installed using pip. This is also misleading, since that graphviz package is just a python wrapper, and the graphviz binaries have to be installed separately for the python wrapper to work.
I had the same problem. I am using Anaconda python on Ubuntu. but it seems that Keras uses system’s python not the Anaconda python. Initially, I installed pydot and graphviz using conda. When I installed pydot and graphviz in system’s python(using /usr/bin/pip install pydot) it worked fine.
I had similar problem with my Keras (without anaconda). I have solved my problem using this way
sudo pip install pydot
sudo pip install graphviz
sudo add-apt-repository ppa:gviz-adm/graphviz-dev
sudo apt-get update
sudo apt-get install graphviz-dev
Install graphviz by brew in osx brew install graphviz
, for ubuntu use apt-get install graphviz
, do not need to install graphviz by pip.
Keras 2.0.6 looks for pydot-ng (better maintained) and then if it’s not found, falls back on pydot. I resolved this issue by installing pydot-ng from source.
If you are using an Anaconda environment, you’d better install pydotplus
and graphviz
via conda install
.
conda install graphviz
conda install pydotplus
Note:
You’d better update your Keras to the newest version (2.0.9+), it can automatically check and choose which one of pydotplus
,pydot-ng
,pydot
to be used. pydot-ng
has been unmaintained for a long time, and it only supports py3.4- and py2.7.
For Anaconda on Mac:
To install this package with conda run:
conda install -c anaconda graphviz
-
Install graphviz
to the system. Download the package from here, or on Mac:
brew install graphviz
-
Install python pydot-ng and graphviz wrapper.
pip install pydot-ng graphviz
conda install -c anaconda pydot-ng #Anaconda user
-
Use pydot-ng
in your code
import pydot_ng as pydot
-
If Keras visualization utils still uses pydot, try to change import pydot
to import pydot_ng as pydot
in visualize_util.py
1)Conda install graphviz
2)pip install graphviz
3)pip install pydot
then:
import os
os.environ["PATH"] += os.pathsep + AppData\Local\Continuum\anaconda3\envs\tensorflow\Library\bin\graphviz'
What I did is followed.
import keras
import pydotplus
from keras.utils.vis_utils import model_to_dot
keras.utils.vis_utils.pydot = pydot
plot_model(your_model_name, to_file='model.png')
That’s worked for me.
On mac Anaconda python=3.6.8
The below works inside a jupyter notebook runing in a jupyter/tensorflow-notebook
docker container.
!conda install -y graphviz pydotplus
import pydotplus
import keras.utils
keras.utils.vis_utils.pydot = pydotplus
keras.utils.plot_model(your_model_name, to_file='model.png', show_shapes=True)
You need to tell keras to use pydotplus
it is nothing related to pydot or graphviz if you have installed via pip.
You should go download graphviz brinary and install.
Don’t forget adding the bin folder into your PATH: C:/Program Files (x86)/Graphviz2.38/bin/
And after that to close and reopen your console.
After installing pydot and graphviz and adding graphviz to the path, you can restart the IDE or terminal. see here.
import keras
import pydotplus
from keras.utils.vis_utils import model_to_dot
keras.utils.vis_utils.pydot = pydot
tf.keras.utils.plot_model(
model,
show_shapes = True,
show_dtype = True,
show_layer_activations = True
)
I’m using Anaconda Python 2.7 on windows 10
I was planning on doing Keras visualization so (whilst spyder was open) I opened the Anaconda command prompt and pip installed graphviz and pydot. Now when I try run the following:
from keras.models import Sequential
or any sort of “from keras.” , I get the error:
ImportError: cannot import name gof
I have uninstalled and reinstalled Keras, Graphviz and pydot. i am using the development version of theano. I cannot find a fix.
P.S
If I uninstall graphviz and pydot, keras works again
EDIT
After uninstalling anaconda and reinstalling it including theano, keras, graphviz and pydot I now get the following error:
from keras.utils.visualize_util import plot
Using Theano backend.
Using gpu device 0: GeForce GTX 970M (CNMeM is disabled, cuDNN not available)
Traceback (most recent call last):
File "<ipython-input-1-65016ddab3cd>", line 1, in <module>
from keras.utils.visualize_util import plot
File "C:Anaconda2libsite-packageskerasutilsvisualize_util.py", line 8, in <module>
raise RuntimeError('Failed to import pydot. You must install pydot'
RuntimeError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.
I used pip install graphviz
and pip install git+https://github.com/nlhepler/pydot.git
The error message is a bit misleading, as you can see here. The problem is that graphviz is not installed.
But you mention that graphviz was installed using pip. This is also misleading, since that graphviz package is just a python wrapper, and the graphviz binaries have to be installed separately for the python wrapper to work.
I had the same problem. I am using Anaconda python on Ubuntu. but it seems that Keras uses system’s python not the Anaconda python. Initially, I installed pydot and graphviz using conda. When I installed pydot and graphviz in system’s python(using /usr/bin/pip install pydot) it worked fine.
I had similar problem with my Keras (without anaconda). I have solved my problem using this way
sudo pip install pydot
sudo pip install graphviz
sudo add-apt-repository ppa:gviz-adm/graphviz-dev
sudo apt-get update
sudo apt-get install graphviz-dev
Install graphviz by brew in osx brew install graphviz
, for ubuntu use apt-get install graphviz
, do not need to install graphviz by pip.
Keras 2.0.6 looks for pydot-ng (better maintained) and then if it’s not found, falls back on pydot. I resolved this issue by installing pydot-ng from source.
If you are using an Anaconda environment, you’d better install pydotplus
and graphviz
via conda install
.
conda install graphviz
conda install pydotplus
Note:
You’d better update your Keras to the newest version (2.0.9+), it can automatically check and choose which one of pydotplus
,pydot-ng
,pydot
to be used. pydot-ng
has been unmaintained for a long time, and it only supports py3.4- and py2.7.
For Anaconda on Mac:
To install this package with conda run:
conda install -c anaconda graphviz
-
Install
graphviz
to the system. Download the package from here, or on Mac:brew install graphviz
-
Install python pydot-ng and graphviz wrapper.
pip install pydot-ng graphviz conda install -c anaconda pydot-ng #Anaconda user
-
Use
pydot-ng
in your codeimport pydot_ng as pydot
-
If Keras visualization utils still uses pydot, try to change
import pydot
toimport pydot_ng as pydot
invisualize_util.py
1)Conda install graphviz
2)pip install graphviz
3)pip install pydot
then:
import os
os.environ["PATH"] += os.pathsep + AppData\Local\Continuum\anaconda3\envs\tensorflow\Library\bin\graphviz'
What I did is followed.
import keras
import pydotplus
from keras.utils.vis_utils import model_to_dot
keras.utils.vis_utils.pydot = pydot
plot_model(your_model_name, to_file='model.png')
That’s worked for me.
On mac Anaconda python=3.6.8
The below works inside a jupyter notebook runing in a jupyter/tensorflow-notebook
docker container.
!conda install -y graphviz pydotplus
import pydotplus
import keras.utils
keras.utils.vis_utils.pydot = pydotplus
keras.utils.plot_model(your_model_name, to_file='model.png', show_shapes=True)
You need to tell keras to use pydotplus
it is nothing related to pydot or graphviz if you have installed via pip.
You should go download graphviz brinary and install.
Don’t forget adding the bin folder into your PATH: C:/Program Files (x86)/Graphviz2.38/bin/
And after that to close and reopen your console.
After installing pydot and graphviz and adding graphviz to the path, you can restart the IDE or terminal. see here.
import keras
import pydotplus
from keras.utils.vis_utils import model_to_dot
keras.utils.vis_utils.pydot = pydot
tf.keras.utils.plot_model(
model,
show_shapes = True,
show_dtype = True,
show_layer_activations = True
)