Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pydot issue #7

Closed
qihongl opened this issue Nov 5, 2017 · 30 comments
Closed

pydot issue #7

qihongl opened this issue Nov 5, 2017 · 30 comments

Comments

@qihongl
Copy link

qihongl commented Nov 5, 2017

Hi, I got the following error message (copied below) when I tried the code, although I do have both pydot and graphviz. I don't think this is an issue with your code. It seems to be a general long-standing issue with pydot. The solutions are discussed here:
1: Theano/Theano#1801
2: pydot/pydot#126

Traceback (most recent call last):
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/keras/utils/vis_utils.py", line 23, in _check_pydot
pydot.Dot.create(pydot.Dot())
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/pydot_ng/init.py", line 1890, in create
'GraphViz's executables not found')
pydot_ng.InvocationException: GraphViz's executables not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "capsulenet.py", line 195, in
plot_model(model, to_file=args.save_dir+'/model.png', show_shapes=True)
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/keras/utils/vis_utils.py", line 131, in plot_model
dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/keras/utils/vis_utils.py", line 52, in model_to_dot
_check_pydot()
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/keras/utils/vis_utils.py", line 27, in _check_pydot
raise ImportError('Failed to import pydot. You must install pydot'
ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work.

@XifengGuo
Copy link
Owner

XifengGuo commented Nov 6, 2017

This is a bug of Keras. You maybe solve this by removing the plot_model function and from keras.utils.vis_utils import plot_model.

Or you can install pydot 1.2.3 by pip.
pip install pydot==1.2.3

I have not re-tested these solutions, if they work, please let me know. Thanks. @QihongL

@qihongl
Copy link
Author

qihongl commented Nov 6, 2017

Yes! I commented those lines since I don't need that model diagram. By the way, thanks for the implementation!

@dlazares
Copy link

FWIW pip install pydot==1.2.3 didn't work for me. Commenting out the lines worked.

@Codeguyross
Copy link

Codeguyross commented Feb 26, 2018

Im running python 3.5. pydot 1.2.3 and i have installed graphviz but plot_model doesn't work. Does anyone have a fix for the plot_model function that corrects whatever is wrong with it? If it helps this is the function in keras.utils.vis_utils.

def plot_model(model,
               to_file='model.png',
               show_shapes=False,
               show_layer_names=True,
               rankdir='TB'):
    """Converts a Keras model to dot format and save to a file.

    # Arguments
        model: A Keras model instance
        to_file: File name of the plot image.
        show_shapes: whether to display shape information.
        show_layer_names: whether to display layer names.
        rankdir: `rankdir` argument passed to PyDot,
            a string specifying the format of the plot:
            'TB' creates a vertical plot;
            'LR' creates a horizontal plot.
    """
    dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
    _, extension = os.path.splitext(to_file)
    if not extension:
        extension = 'png'
    else:
        extension = extension[1:]
    dot.write(to_file, format=extension)

@kostistsaprailis
Copy link

I'm writing this here in case anyone finds this again.

Checking the code for keras/utils/vis_utils.py:

"""Utilities related to model visualization."""
import os

try:
    # pydot-ng is a fork of pydot that is better maintained.
    import pydot_ng as pydot
except ImportError:
    # pydotplus is an improved version of pydot
    try:
        import pydotplus as pydot
    except ImportError:
        # Fall back on pydot if necessary.
        try:
            import pydot
        except ImportError:
            pydot = None


def _check_pydot():
    try:
        # Attempt to create an image of a blank graph
        # to check the pydot/graphviz installation.
        pydot.Dot.create(pydot.Dot())
    except Exception:
        # pydot raises a generic Exception here,
        # so no specific class can be caught.
        raise ImportError('Failed to import pydot. You must install pydot'
                          ' and graphviz for `pydotprint` to work.')

we can see the error raised by the try except clause above. This tries to run a dummy pydot command in to confirm that pydot is installed correctly.
However the first lines how that the code tries to install initially the package pydot_ng then pydotplus and finally if it's not found the pydot package.

So I installed pydot_ng and it worked for me. However I had another issue. The system couldn't find the GraphViz executables:

InvocationException: GraphViz's executables not found

Under Ubuntu 16.04 I ran:

sudo apt-get install graphviz

and it worked.

@AFAgarap
Copy link

Installing graphviz as mentioned by @kostistsaprailis worked for me too.

@Behrad3d
Copy link

Behrad3d commented Mar 27, 2018

For future reference: If you're using a Mac using Pip to install graphviz is not enough. You need to use brew
brew install graphviz

@HemersonTacon
Copy link

On Windows I had to install de binaries (https://graphviz.gitlab.io/_pages/Download/Download_windows.html) and then set the bin folder on PATH environment variable

@sakimilo
Copy link

yup, by install using apt-get works for me as well!
sudo apt-get install graphviz

@abhishek95
Copy link

abhishek95 commented Jun 4, 2018

I did the following for Mac:-

  1. Installed pydot_ng (pip install pydot_ng)
  2. Go to keras/utils/vis_util.py (this is the file showing error)

You will see something like this.
try:

import pydot
except ImportError:
pydot = None

  1. Change import pydot to import pydot_ng as pydot

And voila!

@manuelblancovalentin
Copy link

None of the above worked for me as none of them were "immediately" applied to my jupyter-notebook session. As I was running a very long code on jupyter and COULD NOT LOSE MY DATA, BY ANY CHANCE, installing these modules was not sufficient, as even reloading the modules would not make the trick. Instead I needed to restart the kernel, or spyder-ide (which would make me lose my data).

So I simply opened a terminal (ubuntu here) and edited the /keras/utils/vis_util.py file (for me, using anaconda, it was under: /home/<my_user>/miniconda2/lib/python2.7/site-packages/keras/utils/vis_util.py) and commented the "check_pydot()" call inside the "model_to_dot" function definition.

After that, reloading the code inside jupyter-notebook was enough to render the net architecture WITHOUT HAVING TO RESTART THE JUPYTER KERNEL, AND WITHOUT LOSING THE DATA. No other errors appeared, btw, and the net was rendered properly.

@hamk3010
Copy link

Importing pydot-ng wasn't working for me since I was using python 3.6

UnsatisfiableError: The following specifications were found to be in conflict:
- pydot-ng -> python=2.7
- python=3.6
Importing pydotplus worked for me

@rgkimball
Copy link

I also ran into kostistsaprailis's issue but I'm not on a *nix so the previous answers didn't apply.

If you're on a Windows machine, obtaining the GraphViz library is slightly less convenient without a native package manager. Looking at pydot_ng's find_graphviz() , it first checks your registry, then your path via os.environ but these may not be configured if you downloaded the source instead. If you install GraphViz to: C:\Program Files\att\GraphViz then it will locate the executables within the bin subdirectory there.

@dmpierre
Copy link

dmpierre commented Oct 17, 2018

For those still struggling:

First:

$ pip install pydot-ng

As explained, pydot-ng is actually better maintained.

Second, go to where your keras repo is set up. E.g. mine is a virtualenv at:

$ cd ~/.virtualenvs/venv-name/lib/python3.6/site-packages/keras/utils/

Third, at line 11 of vis_utils.py, change to:

try:
    import pydot_ng as pydot
except ImportError:
    pydot = None

Ensure graphviz is installed (using brew, apt-get, yum or whatever your OS is), this should work :)

@JialieY
Copy link

JialieY commented Oct 27, 2018

Download the dll from https://graphviz.gitlab.io/_pages/Download/Download_windows.html

and then append the bin folder path in your system path, it will work.

The problem is they only check the dot.exe in the system path or machine registration.

@JialieY
Copy link

JialieY commented Oct 27, 2018

This is a bug, keras should fix it.

@Hippyu
Copy link

Hippyu commented Apr 13, 2019

Ensure you have already installed graphviz, pydot, pydotplus.
pydot-ngis not satisfied with python=3.6.8, but python=3.4, where I found by conda info pydot-ng
So what I did is followed.

import pydotplus
from keras.utils.vis_utils import model_to_dot
import keras
keras.utils.vis_utils.pydot = pydot
plot_model(autoencoder, to_file='model.png')

that's worked for me.
On mac Anaconda. python=3.6.8

@Jeff-Winchell
Copy link

What a cluster$Q$5

Testing code, encapsulation, robustness to change are all software engineering problems solved decades ago. Data science has thrown them all into the garbage can in order to push out partially working code faster. No thank you.

@yijiejiang
Copy link

yijiejiang commented Jun 14, 2019

I got this error recently, I install the pydot and pydot-ng , and I brew install grapgviz in my env, I can see these packages in my interpreter, but it do not work even if I try edit vis_utils.py, I solve the problem by using sudo apt-get install grapgviz.

@matheushent
Copy link

I am using windows 10 and had the same issue. First, I commented the "_check_pydot()" as @manuelblancovalentin mentioned above. After that, the code kept doesn't run. So I did what @JialieY said and violà, everything right.

@Furjoza
Copy link

Furjoza commented Sep 27, 2019

This is a bug of Keras. You maybe solve this by removing the plot_model function and from keras.utils.vis_utils import plot_model.

Or you can install pydot 1.2.3 by pip.
pip install pydot==1.2.3

I have not re-tested these solutions, if they work, please let me know. Thanks. @QihongL

Thanks. I have googled half of the internet to find this comment to downgrade the package. Thank you very much.

@squallssck
Copy link

squallssck commented Nov 13, 2019

For those still struggling:

First:

$ pip install pydot-ng

As explained, pydot-ng is actually better maintained.

Second, go to where your keras repo is set up. E.g. mine is a virtualenv at:

$ cd ~/.virtualenvs/venv-name/lib/python3.6/site-packages/keras/utils/

Third, at line 11 of vis_utils.py, change to:

try:
    import pydot_ng as pydot
except ImportError:
    pydot = None

Ensure graphviz is installed (using brew, apt-get, yum or whatever your OS is), this should work :)

install pydot_ng solves my problem :) ... again , on my win10 env i have to install graphvis.msi , set bin to path and then pip install pydot_ng to make keras plot_model to work.

@tzamalisp
Copy link

the solution that @kostistsaprailis suggested worked fine for me!

@hessam94
Copy link

install graphviz on windows and add bin to env var wokred for me,

@micuevisa
Copy link

Tried everything and nothing works.
uninstalled all pydot related modules and graphiviz, then reinstalled in the following order:
#pydot_ng
#pydot
#pydotplus
#graphviz
#On Windows I had to install de binaries (https://graphviz.gitlab.io/_pages/Download/Download_windows.html) which also sets the PATH variable

It's broken.

@Calandiel
Copy link

Four years later and it's still broken

@Daniel-Chin
Copy link

Daniel-Chin commented Dec 4, 2021

I am on Windows and I finally got it working.

The problem was that restarting the kernal in Jupyter Notebook does not restart the system shell, so %PATH% was outdated!!!

So the complete solution on Windows:

  • install graphviz binaries
  • add the binaries to %PATH%
  • install pydot_ng
  • restart the Jupyter Notebook server!

(What happened: Windows caches %PATH% at the launch of the shell. Restarting the kernal only starts a new python session in the same shell. So even if you start a new kernal and import os again, os will still contain the old %PATH%.)

@usmandroid
Copy link

Ensure you have already installed graphviz, pydot, pydotplus. pydot-ngis not satisfied with python=3.6.8, but python=3.4, where I found by conda info pydot-ng So what I did is followed.

import pydotplus
from keras.utils.vis_utils import model_to_dot
import keras
keras.utils.vis_utils.pydot = pydot
plot_model(autoencoder, to_file='model.png')

that's worked for me. On mac Anaconda. python=3.6.8

This worked for me on Python 3.10 on Mac M1 minoforge3 environment.! Kudos to you! Before running this I made sure that all the packages are installed. No need to edit the vis_utils file!
pydot — 1.4.2
pydot-ng — 2.0.0
pydotplus — 2.0.2
keras — 2.9.0
python — 3.10.5

@hadiloghman
Copy link

I am on Windows and I finally got it working.

The problem was that restarting the kernal in Jupyter Notebook does not restart the system shell, so %PATH% was outdated!!!

So the complete solution on Windows:

  • install graphviz binaries
  • add the binaries to %PATH%
  • install pydot_ng
  • restart the Jupyter Notebook server!

(What happened: Windows caches %PATH% at the launch of the shell. Restarting the kernal only starts a new python session in the same shell. So even if you start a new kernal and import os again, os will still contain the old %PATH%.)

Great!!! worked for me. TNX

@maurlco
Copy link

maurlco commented Feb 14, 2023

Ensure you have already installed graphviz, pydot, pydotplus. pydot-ngis not satisfied with python=3.6.8, but python=3.4, where I found by conda info pydot-ng So what I did is followed.

import pydotplus
from keras.utils.vis_utils import model_to_dot
import keras
keras.utils.vis_utils.pydot = pydot
plot_model(autoencoder, to_file='model.png')

that's worked for me. On mac Anaconda. python=3.6.8

This worked for me on Python 3.10 on Mac M1 minoforge3 environment.! Kudos to you! Before running this I made sure that all the packages are installed. No need to edit the vis_utils file! pydot — 1.4.2 pydot-ng — 2.0.0 pydotplus — 2.0.2 keras — 2.9.0 python — 3.10.5

This worked for me - I installed the packages as above, the pydotplus was missing for me. It works even with the file vis_utils edited. Then import the above package and "keras.utils.vis_utils.pydot = pydot" on my Jupiter notebook. Thank you @usmandroid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests