77

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?

bit_scientist
  • 1,496
  • 2
  • 15
  • 34
  • 1
    Please see https://stackoverflow.com/a/47209738/1959808 – 0 _ Dec 02 '17 at 19:54
  • 1
    Possible duplicate of [keras plot\_model tells me to install pydot](https://stackoverflow.com/questions/47188756/keras-plot-model-tells-me-to-install-pydot) – 0 _ Dec 02 '17 at 19:54
  • 1
    @loannis Filippidis, thanks, yes, it says _The path(s) to the installed GraphViz executables (dot, neato, etc.) need to be in the PATH environment variable, in order for pydot to find them. pydot used to search for those executables in earlier versions, but not any more._ If you mean this, I don't know how to do this, sorry for that, Can you be more specific on what to do step by step? – bit_scientist Dec 02 '17 at 20:43
  • The answer mentioned above links to relevant information for path configuration. – 0 _ Dec 02 '17 at 22:16
  • yes I saw, but I couldn't get it work. What I am saying is I don't know how to put the paths to installed GraphViz exucutables (dot, neasto, etc) in the Path environment. I am a new user of windows actually – bit_scientist Dec 02 '17 at 22:41
  • @loannisFilippidis, I found dot and neato executables in my _C:\Users\username\Anaconda3\envs\tfcnn\Library\bin\graphviz_ and added this to **system properties>environment varaibles>system variables>edit**. It is not working though! – bit_scientist Dec 04 '17 at 09:16
  • I am not a Windows user. Perhaps rebooting is needed for the new path to take effect. On Linux systems usually the path is configured via `~/.bashrc`. – 0 _ Dec 04 '17 at 09:31

23 Answers23

56

The following commands solved the problem for me

  1. pip install pydot
  2. pip install pydotplus
  3. sudo apt-get install graphviz
iun1x
  • 1,033
  • 11
  • 12
  • 2
    And in Windows, because I had already installed conda, I used `conda install graphviz` instead of `sudo apt-get install graphviz` and it worked. Thank you! – kodfire Jul 23 '22 at 11:58
  • Also, as mentioned below - restart the kernel. – dainys Jan 30 '23 at 09:13
34

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.

omatai
  • 3,448
  • 5
  • 47
  • 74
  • 1
    Try looking if GraphViz is installed from conda. If so, remove it and install it from pip. This is what helped me solving this. – shahar_m Nov 29 '20 at 13:09
  • 1
    `I had to close and reopen that to refresh the PATH, but then plot_model() produced output fine.` This is the _pro_ tip. – marcelovca90 Mar 01 '22 at 23:02
  • If you are working in Pycharm, make sure to use the system interpreter of python, not the virtual interpreter that Pycharm automatically binds to every python project. File > Settings> Project aaa > Python interpreter , then choose the local bin directory, e.g. C:\Python310\python.exe – Michael Jun 29 '23 at 06:20
20

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

Sadman Sakib
  • 557
  • 3
  • 10
19

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.

Baya Lina
  • 457
  • 4
  • 11
10

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

Jérôme Braure
  • 101
  • 1
  • 2
6

Use next command to install them:

sudo apt install python-pydot python-pydot-ng graphviz 
Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
Horatiu
  • 375
  • 3
  • 6
6

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)
Simen
  • 417
  • 6
  • 13
4

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 
akokskis
  • 1,486
  • 15
  • 32
2

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)
Biranchi
  • 16,120
  • 23
  • 124
  • 161
2

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.

solidsteam
  • 21
  • 1
2

Maybe it to late. But i think it will help to someone.I use ubuntu 22.04. In my case first i installed:

sudo apt install graphviz
pip install pydot
pip install pydotplus
pip install graphviz

The second step is restart the computer.

Tip-Rok
  • 21
  • 3
2

I recently encountered this issue, and none of the suggested solutions worked for me because I am using a Mac. So, if you're a Mac user like me, use the commands below:

pip install pydot
pip install pydotplus
pip install graphviz

brew install graphviz

You need to install graphviz with brew to be able to use the plot_model

Aiden Pearce
  • 243
  • 2
  • 13
1

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.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
1

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

Lakwin Chandula
  • 159
  • 1
  • 3
  • 15
0

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
InspiredCat
  • 27
  • 2
  • 7
0

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

VVK
  • 11
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 19 '21 at 07:28
0
  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

0

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)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '22 at 11:47
0

I'm working in Jupyter Notebook. I tried everything in this thread and others and nothing worked. And then I found this and it worked for me:

  1. close jupyter notebook.
  2. open new Anaconda prompt.
  3. conda install --channel conda-forge pygraphviz

Open Jupyter notebook and make sure you have the following imports:

  • from tensorflow.keras.utils import plot_model
  • import pydot
  • import pydotplus
  • from pydotplus import graphviz
0

Take care when installing the Pycharm module, do not forget to note that you are in the conda environment.

packages layout example

James Risner
  • 5,451
  • 11
  • 25
  • 47
-1

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

  • This indicates that you have imported both keras and tensorflow as tf imported. The current recommendation is to NOT import keras and instead use tf.keras whenever you need access to keras functions. – Leland Hepworth Jan 14 '21 at 16:31
-1

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

-1

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)
  • 8
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Nov 03 '20 at 11:03
  • This answer shows how to use the plot_model function, which is something that OP has demonstrated they know how to do. The question is about resolving a pydot and graphviz installation error. – Leland Hepworth Jan 14 '21 at 16:33