30

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:\Anaconda2\lib\site-packages\keras\utils\visualize_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

ishido
  • 4,065
  • 9
  • 32
  • 42
  • Can you paste the complete error message from python? – Dr. Snoopy Apr 27 '16 at 10:54
  • Keras is now running again. But without the packages Graphviz and Pydot installed. When I tried uninstalling them earlier on my python crashed every time I opened it, which is why I'm too afraid to attempt re-installation of either of the packages. Could it be that one of them gets in the way of Keras? – ishido Apr 27 '16 at 12:02
  • With your edit, the question has now changed significantly, you should edit the title to reflect the new question. – Dr. Snoopy Apr 27 '16 at 12:39
  • I've answered similar question with step-by-step debugging technique at https://stackoverflow.com/a/47433183/1074998 – 林果皞 Nov 22 '17 at 14:52

13 Answers13

51

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.

Stephane Bersier
  • 710
  • 7
  • 20
Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • I had similar issue. The only answer that solved my problem among many similar questions. Thanks! – CentAu Aug 12 '16 at 13:55
  • 4
    thanks, the error message is misleading, when sudo yum install graphviz, it fixed – whb_zju Feb 14 '17 at 03:31
  • 10
    sudo apt-get install graphviz – FacePalm Feb 21 '17 at 12:21
  • 15
    In Windows 10, download the .msi installer from http://www.graphviz.org/Download_windows.php, run it. When installation is complete make sure that you add the path to the GraphViz binaries (C:\Program Files (x86)\Graphviz2.38\bin) to the windows PATH variable. You may need to restart your Python kernel for it to pick up the path properly. – DrMcCleod Jun 22 '17 at 13:54
  • run `apt-get install graphviz` on debian `yum install graphviz` on CentOS – Zahra Jun 22 '18 at 19:36
  • 3
    for windows just run, `conda install graphviz` and `pip install pydot` – Muhammad Younus Feb 28 '19 at 05:34
10

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.

William
  • 4,258
  • 2
  • 23
  • 20
4

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

Yupei
  • 411
  • 1
  • 5
  • 9
  • Extended view `plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)` – itsergiu Apr 17 '19 at 07:42
3

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.

alhanaei
  • 31
  • 2
  • 1
    But I am on Windows and use Anaconda. I understand there is one python in my case. Even then it fails. Any ideas on what could be wrong ? – tauseef_CuriousGuy Jul 29 '16 at 10:54
3

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.

jok
  • 263
  • 2
  • 6
2

Install graphviz by brew in osx brew install graphviz, for ubuntu use apt-get install graphviz, do not need to install graphviz by pip.

phd
  • 82,685
  • 13
  • 120
  • 165
yogesh
  • 33
  • 1
  • 7
1

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
Eka
  • 14,170
  • 38
  • 128
  • 212
1

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'

amit pandey
  • 86
  • 1
  • 8
0

For Anaconda on Mac:

To install this package with conda run:

conda install -c anaconda graphviz

saneryee
  • 3,239
  • 31
  • 22
0
  1. Install graphviz to the system. Download the package from here, or on Mac:

    brew install graphviz
    
  2. Install python pydot-ng and graphviz wrapper.

    pip install pydot-ng graphviz
    conda install -c anaconda pydot-ng #Anaconda user
    
  3. Use pydot-ng in your code

    import pydot_ng as pydot
    
  4. If Keras visualization utils still uses pydot, try to change import pydot to import pydot_ng as pydot in visualize_util.py

Shih-Wen Su
  • 2,589
  • 24
  • 21
0

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

BenCaldwell
  • 323
  • 1
  • 15
0

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.

song
  • 1
  • 1
  • 2
0

After installing pydot and graphviz and adding graphviz to the path, you can restart the IDE or terminal. see here.