ChatGPT解决这个技术问题 Extra ChatGPT

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?

@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?
The answer mentioned above links to relevant information for path configuration.
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

i
iun1x

The following commands solved the problem for me

pip install pydot pip install pydotplus sudo apt-get install graphviz


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!
o
omatai

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.


Try looking if GraphViz is installed from conda. If so, remove it and install it from pip. This is what helped me solving this.
I had to close and reopen that to refresh the PATH, but then plot_model() produced output fine. This is the pro tip.
S
Sadman Sakib

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


B
Baya Lina

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.


This worked perfectly for me
This works for me too :-)
it worked for me as well. I first installed these packages with pip but that did not work. conda install worked
you are a hero!
J
Jérôme Braure

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


T
Tshilidzi Mudau

Use next command to install them:

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

E: Unable to locate package python-pydot-ng
a
akokskis

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 

Just conda install -c conda-forge pydot graphviz works
B
Biranchi

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)

S
Simen

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

Install Graphviz 32 bit (64 bit didn't work) Add Graphviz path C:\Program Files (x86)\Graphviz\bin to system's and user's PATH environment variables 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)

s
solidsteam

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.


T
Tshilidzi Mudau

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.


L
Lakwin Chandula

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


I
InspiredCat

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

V
VVK

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


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.
S
Shraddha Mishra

Install Graphviz from https://www.graphviz.org/download/ Import: from tensorflow.python.keras.utils.vis_utils import model_to_dot from tensorflow.python.keras.utils.vis_utils import plot_model


S
SaralaKumarage

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.
M
Muhammad Alhaji Dauda

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.
佚名

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


A
Alexander Pereyaslavtsev

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)

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

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now