ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work

I have seen similar issue but it is not solved either, so I decided to ask.

I am trying to visualize my model in keras using

from keras.utils import plot_model
plot_model(model, to_file='model.png')

First, it showed error

ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.

Accordingly, I installed pydot and graphviz through Anaconda prompt activating my environment using

conda install -c https://conda.binstar.org/t/TOKEN/j14r pydot
conda install -c https://conda.binstar.org/t/TOKEN/j14r graphviz

Then, I closed spyder and reopened it. When I run code snippet, it is still showing the same error. What am I missing?


The following commands solved the problem for me

  1. pip install pydot
  2. pip install pydotplus
  3. sudo apt-get install graphviz

It seems like you are using Windows. In that case, see this SO Q&A stream and/or this Keras issue on gitub.

Following hints from both sources, it seemed likely there was an install error and/or a path error. I used pip uninstall on all related packages, then:

pip install pydot
pip install pydotplus
pip install graphviz

Then:

  • Download and install graphviz binaries from here
  • Add path to graphviz bin folder in system PATH

I was running a python script myscript.py within a Windows cmd window. I had to close and reopen that to refresh the PATH, but then plot_model() produced output fine.


Solution found from: https://github.com/XifengGuo/CapsNet-Keras/issues/69#issuecomment-483273641

I followed the advice of uninstalling and reinstalling pydot + pydotplus and that successfully solved the issue on my Windows 10 machine using Anaconda 3.

conda uninstall pydot
conda uninstall pydotplus
conda uninstall graphviz

then

conda install pydot
conda install pydotplus

Note: installing pydot also installed graphviz