keras: AttributeError: module 'pydot' has no attribute 'find_graphviz'

My os is Ubuntu 14.04 and python version is 3.5. I just simply did what the document said:

from keras.utils.visualize_util import plot

And what I got is:

   ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-90-65016ddab3cd> in <module>()
----> 1 from keras.utils.visualize_util import plot

/home/lcc/anaconda3/envs/sensequant/lib/python3.5/site-packages/keras/utils/visualize_util.py in <module>()
      5     # fall back on pydot if necessary
      6     import pydot
----> 7 if not pydot.find_graphviz():
      8     raise RuntimeError('Failed to import pydot. You must install pydot'
      9                        ' and graphviz for `pydotprint` to work.')

AttributeError: module 'pydot' has no attribute 'find_graphviz'

Indeed I have ensured that pydot, pydotplus, graphviz have been installed as I could import them properly. Do you know how to fix it? Thank you in advance!

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 16
  • Comments: 35 (6 by maintainers)

Most upvoted comments

pydot-ng will give you pydot that satisfies the keras package, use pip install pydot-ng to install it in your virtualenv.

On ubuntu you need to have the “dot” executable in your PATH, you can do this by installing the graphviz package: apt-get install graphviz

You can try to install pydot-ng.

I faced similar problem and executing the following commands solved my problem.

pip install pydot-ng
sudo apt-get install graphviz

On Mac: If you are working in a virtual environment with Anaconda (always do that to avoid messing up the python in your OS):

  1. source activate virtualenv_name
  2. conda install graphviz
  3. pip install pydot-ng

And run your script from terminal. As a sanity check (in terminal):

  1. python
  2. import pydot_ng as pydot
  3. pydot.find_graphviz()

The output should be a string similar to: “{‘dot’: ‘your_path/bin/dot’, ‘twopi’: …”

But, @sookinoby is right, model.summary() is much more useful.

Here is how I fix the problem: sudo pip install pydot==1.0.2 --upgrade It seems pydot don’t provide a function named find_graphviz anymore.

http://stackoverflow.com/questions/36869258/how-to-use-graphviz-with-anaconda-spyder

Downloading Graphviz from their website (msi version in my case given I’m on windows) and adding C:\Program Files (x86)\Graphviz2.38\bin to my system variables in PATH worked for me.

on Mac, the following commands fixed the issue:

sudo pip install pydot-ng
brew install graphviz

@tboquet Thanks for your help! But this time I got this error: RuntimeError: Failed to import pydot. You must install pydot and graphviz forpydotprintto work. But I have installed pydot and graphviz already. Do you have any idea about that?

@oeway The same error still exists…

I have this issue with python3 on ubuntu 16.04. Since I know I have the necessary packages installed, I do the following to avoid the error:

import pydot
pydot.find_graphviz = lambda: True

I have this issue with ubuntu 16.04 But raises import error:

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

This can help me

pip install pydot-ng
sudo apt-get install graphviz

Thanks @Tahsin-Mayeesha . This is the right solution.

First, the pydot will find_graphviz, in line 7 of visualize_util.py. Then, it will check these PATH to judge whether the graphviz exists, in line 605 of pydot_ng/__init__.py.

'/usr/bin', '/usr/local/bin',
            '/opt/local/bin',
            '/opt/bin', '/sw/bin', '/usr/share',
            '/Applications/Graphviz.app/Contents/MacOS/'

So, we should install graphviz by these way:

  1. download APP, this will put to /Applications/Graphviz.app/Contents/MacOS/;
  2. command brew install graphviz, this will create the link to /usr/local/bin.

no not stale unless a human says so

For window, this because python can’t find graphviz path, in pydot_ng init.py It just search path for linux and Mac user, will can fix this problem by add path

for path in (
            '/usr/bin', '/usr/local/bin',
            '/opt/local/bin',
            '/opt/bin', '/sw/bin', '/usr/share',
            '/Applications/Graphviz.app/Contents/MacOS/'
            ):
        progs = __find_executables(path)
        if progs is not None:
            print("Used path")
            return progs

We should modify to

    for path in (
            '/usr/bin', '/usr/local/bin',
            '/opt/local/bin',
            '/opt/bin', '/sw/bin', '/usr/share',
            '/Applications/Graphviz.app/Contents/MacOS/'
            ):
        myGraphVizPath='D:\\Programming\\Graphviz2.38\\bin'#my graphviz path 
        progs = __find_executables(myGraphVizPath)
        if progs is not None:
            print("Used path")
            return progs

    # Failed to find GraphViz
    return None

http://blog.csdn.net/u014376030/article/details/69945238

try pydotplus

Same issue here… Seems that pydot-ng supports up to Python 3.4? And pydot 1.1.0 not available in conda… For Keras to work properly with Python 3.5, any consideration to embed pydotplus?

pydot pre 1.2.0 do not support python 3…

Could you try to install from here: https://github.com/nlhepler/pydot

As you can see from here https://github.com/nlhepler/pydot/blob/master/pydot/__init__.py#L474 , they do have find_graphviz function.