'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?



Solution 1:[1]

The following commands solved the problem for me

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

Solution 2:[2]

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 3:[3]

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

Solution 4:[4]

I solved this problem by installing :

conda install graphviz
conda install pydot
conda install pydotplus

PS: i called plot_model with:

from tensorflow.keras.utils import plot_model

and it's working now.

Solution 5:[5]

Restarting the Kernel solved the problem for me, without needing pydot-ng.

Solution 6:[6]

Use next command to install them:

sudo apt install python-pydot python-pydot-ng graphviz 

Solution 7:[7]

These commands work for me. I did:

conda install -c https://conda.binstar.org/t/TOKEN/j14r pydot
conda install -c https://conda.binstar.org/t/TOKEN/j14r graphviz
sudo apt install python-pydot python-pydot-ng graphviz 

Solution 8:[8]

This worked for me

import keras.utils.vis_utils
from importlib import reload
reload(keras.utils.vis_utils)


from keras.utils.vis_utils import plot_model    
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)

Solution 9:[9]

Using TensorFlow 2.3.0 on Windows 10 without Anaconda, the following (finally) worked for me:

  1. Install Graphviz 32 bit (64 bit didn't work)
  2. Add Graphviz path C:\Program Files (x86)\Graphviz\bin to system's and user's PATH environment variables
  3. Install pydot-ng which is the preferred pydot library used by TensorFlow 2.3.0
from tensorflow.keras.utils import plot_model

# model = Model(...)

plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)

Solution 10:[10]

For me, all I had to do was install graphviz and pydot:

On Python3:

pip3 install pydot-ng
pip3 install graphviz

On Python2:

pip3 install pydot-ng
pip3 install graphviz

That resolved the error for me.

Solution 11:[11]

on win10 anaconda3 start your command prompt with run as administrator then

conda install graphviz

This gives you graphviz2.38 which works. This beats the way downloading installers from https://graphviz.gitlab.io/download/#windows, which didn't work on my machine. Then you can pip install pydot to make sure you have it. Then restart kernel and it should be fine. If not, pip install graphviz, for it seems to be a necessary python wrapper. I also tried pip intsall pydot-ng pydotplus before the conda install command above. They didn't help at that time.

Solution 12:[12]

This worked for me:

Install in my virtual environment (Python 3):

pip3 install pydot_ng

Install at machine level (it didn't work if installed only in environment):

sudo apt-get install graphviz

To import it:

import pydot_ng as pydot

Solution 13:[13]

Try this.

import keras
import pydot
import pydotplus
from pydotplus import graphviz
from keras.utils.vis_utils import plot_model
from keras.utils.vis_utils import model_to_dot
keras.utils.vis_utils.pydot = pydot

This worked for me. Check this out -> https://github.com/XifengGuo/CapsNet-Keras/issues/69

Solution 14:[14]

Install pydot and pydotplus using pip. Download exe for graphviz and install. Add graphviz to PATH. Restart and check. It will work.

Solution 15:[15]

  1. Install Graphviz from https://www.graphviz.org/download/

  2. Import:

    from tensorflow.python.keras.utils.vis_utils import model_to_dot

    from tensorflow.python.keras.utils.vis_utils import plot_model

Solution 16:[16]

pip install pydot pip install jupyterlab

go to https://graphviz.org/download/ and install graphviz

import os #os.environ["PATH"] += os.pathsep + 'C:/Program Files/Graphviz/bin/'

tf.keras.utils.plot_model(model = titanic_preprocessing , rankdir="LR", dpi=72, show_shapes=True)

Solution 17:[17]

Even after installing pydot, pydotplus and graphviz I was met with an error that dot cannot be found in environment PATH

so I installed graphviz-2.38.msi from https://graphviz.gitlab.io/download/

But, the problem still persisted until I switched from **keras.utils.plot_model** to **tf.keras.utils.plot_model**

See image, Plot model now works

Solution 18:[18]

For jupyter notebook after installing the requirements restart the notebook. It worked for me.

Solution 19:[19]

It works in Spuder IDE. The main idea is to reduce the number of imported libraries.

from keras.utils.vis_utils import pydot
from keras.utils.vis_utils import plot_model

plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)

Solution 20:[20]

Just restart your console after installing those libraries, it should be worked properly.