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

KerasGraphvizImporterrorPydot

Keras Problem Overview


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?

Keras Solutions


Solution 1 - Keras

The following commands solved the problem for me

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

Solution 2 - Keras

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 - Keras

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 - Keras

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 - Keras

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

Solution 6 - Keras

Use next command to install them:

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

Solution 7 - Keras

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 - Keras

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 - Keras

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 - Keras

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 - Keras

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 - Keras

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 - Keras

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 - Keras

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

Solution 15 - Keras

  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 - Keras

> 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 - Keras

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 - Keras

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

Solution 19 - Keras

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 - Keras

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbit_scientistView Question on Stackoverflow
Solution 1 - Kerasiun1xView Answer on Stackoverflow
Solution 2 - KerasomataiView Answer on Stackoverflow
Solution 3 - KerasSadman SakibView Answer on Stackoverflow
Solution 4 - KerasBaya LinaView Answer on Stackoverflow
Solution 5 - KerasJérôme BraureView Answer on Stackoverflow
Solution 6 - KerasHoratiuView Answer on Stackoverflow
Solution 7 - Kerasuser11417332View Answer on Stackoverflow
Solution 8 - KerasBiranchiView Answer on Stackoverflow
Solution 9 - KerasSimenView Answer on Stackoverflow
Solution 10 - KerasTshilidzi MudauView Answer on Stackoverflow
Solution 11 - KerassolidsteamView Answer on Stackoverflow
Solution 12 - Kerasberta gpView Answer on Stackoverflow
Solution 13 - KerasLakwin ChandulaView Answer on Stackoverflow
Solution 14 - KerasVVKView Answer on Stackoverflow
Solution 15 - KerasShraddha MishraView Answer on Stackoverflow
Solution 16 - KerasSaralaKumarageView Answer on Stackoverflow
Solution 17 - KerasMuhammad Alhaji DaudaView Answer on Stackoverflow
Solution 18 - Kerasuser13959036View Answer on Stackoverflow
Solution 19 - KerasAlexander PereyaslavtsevView Answer on Stackoverflow
Solution 20 - KerasMawandaView Answer on Stackoverflow