GraphViz not working when imported inside PydotPlus (`GraphViz's executables not found`)

I've been trying to make these packages work for quite some time now but with no success. Basically the error is:

GraphViz's Executables not found

EDIT: I had not posted a terminal log with the error originally. I'm using Ubuntu now so I won't be able to reproduce the exact same error I got in the past (a year ago, so far away in the past...). However, I've been experiencing a similar --- if not the same --- error in my current setup; even while using a virtual environment with pipenv. The error seems to come from lines that were described in @张乾元's answer:

Traceback (most recent call last):
  File "example.py", line 49, in module
    Image(graph.create_png())
  File "/home/philippe/.local/lib/python3.6/site-packages/pydotplus/graphviz.py", line 1797, in lambda
    lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
  File "/home/philippe/.local/lib/python3.6/site-packages/pydotplus/graphviz.py", line 1960, in create
    'GraphViz\'s executables not found')
pydotplus.graphviz.InvocationException: GraphViz's executables not found

I've tried to install GraphViz via 2 different ways: via pip install graphviz and through the .msi package (and also tried to install pydot, pydotplus and graphviz in many different orders).

The code I'm trying to run is simply a dot-to-png converter for the Iris Dataset.

from sklearn.tree import DecisionTreeClassifier
import sklearn.datasets as datasets
from sklearn.externals.six import StringIO
from sklearn.tree import export_graphviz

import pandas as pd
import pydotplus

from IPython.display import Image

iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns = iris.feature_names)
y = iris.target

dtree = DecisionTreeClassifier()
dtree.fit(df,y)

dot_data = StringIO()
export_graphviz(
    dtree, 
    out_file = dot_data,
    filled = True, 
    rounded = True,
    special_characters = True
)
graph_1 = pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph_1.create_png())

In Jupyter Notebooks and in Atom, the system seems to be looking for GraphViz inside pydotplus, as it points to ~\Anaconda3\lib\site-packages\pydotplus\graphviz.py. Shouldn't it be the other way around?

Lastly, I just want to point out that I've already tried adding GraphViz's path to the system's PATH using C:\Users\Philippe\Anaconda3\Library\bin\graphviz.

Topic graphviz jupyter anaconda visualization python

Category Data Science


As everyone already described, it is required to install the graphviz. However, instead of pip, installing with apt-get solves the issue for me.

sudo apt-get install graphviz

Navigated to anaconda Directory

C:\ProgramData\Anaconda3\Library\bin

I realised Graphviz was already installed, all I did was to go to

Control Panel > System and Security > System > Advanced System Settings > Environment Variables > Path > Edit

and added

"C:\ProgramData\Anaconda3\Library\bin\graphviz"


Updating through running in the anaconda prompt:

conda install python-graphviz

till version 2.38 (2019.10.16-0) did the trick for me.


I had the same problem and did everything suggested on this and other forums and nothing worked out. The following instructions will 100% solve this problem if you are using Windows and Python 3:

  1. Install pydotplus via conda: conda install pydotplus

  2. Install graphviz independently conda install python-graphviz (if you already did these 2 steps, go straight to step 3)

  3. Assuming you already have graphviz and pydotplus installed, find the graphviz.py file in your pydotplus installation directory, in my case, it was in the following path: C:\Users\Acevedo\Anaconda3\Lib\site-packages\pydotplus\graphviz.py

  4. Open graphviz.py and find this block in line 606

    # Method 3 (Windows only)
    if os.sys.platform == 'win32':
    
        # Try and work out the equivalent of "C:\Program Files" on this
        # machine (might be on drive D:, or in a different language)
        if 'PROGRAMFILES' in os.environ:
            # Note, we could also use the win32api to get this
            # information, but win32api may not be installed.
            path = os.path.join(
                os.environ['PROGRAMFILES'], 'ATT', 'GraphViz', 'bin'
            )
        else:
            # Just in case, try the default...
            path = r"C:\Program Files\att\Graphviz\bin"
    
        progs = __find_executables(path)
    
  5. Comment the if/else part and hardcode the path of your graphviz installation directory, inside which should be the executables (dot.exe, circo.exe, gvedit.exe, etc.). The new code has to look like this:

    # Method 3 (Windows only)
    if os.sys.platform == 'win32':
    
        # Try and work out the equivalent of "C:\Program Files" on this
        # machine (might be on drive D:, or in a different language)
        """if 'PROGRAMFILES' in os.environ:
            # Note, we could also use the win32api to get this
            # information, but win32api may not be installed.
            path = os.path.join(
                os.environ['PROGRAMFILES'], 'ATT', 'GraphViz', 'bin'
            )
        else:
            # Just in case, try the default...
            path = r"C:\Program Files\att\Graphviz\bin"
            """
        path = r"C:\Users\Acevedo\Anaconda3\Library\bin\graphviz"
    
        progs = __find_executables(path)
    
  6. Save the graphviz.py file and everything should work just fine :)


What worked for my use case: Generating model diagrams in Django. But it can also be extended to generate diagrams for any other applications.

I installed the GraphViz for viewing graph from .dot file. Can be installed from graphviz.org.

  1. Create a dot file associated with the project:

    python manage.py graph_models -a > dotfile.dot
    

    Or you could create the .dot files for multiple apps. Reference: Using django-extensions to visualize the database diagram in django application, by Thomas.

  2. Now you just need to view dotfile.dot. So where should I view that as an image?

    • Now open the file gvedit.exe (I don't know what's corresponding file in Linux) in the insalled path of the application.
      • For windows: C:\Program Files (x86)\Graphviz2.38\bin\gvedit.exe.
  3. Now run gvedit.exe and open .dot file created so far.


In my case I am able to find graphviz executables manually in anaconda3\Library\bin\graphviz, but I still would get the GraphViz's Executables not found error.

I have unsuccessfully tried zhangqianyuan's suggestion as well as specific orders of module installation and using python-graphviz (official solution, widely discussed here). Only thing I didn't try was tampering with my PATH variable system-wide.

A method that worked for me was inserting these lines in my code (before the graphviz related functions):

import os

os.environ['PATH'] = os.environ['PATH']+';'+os.environ['CONDA_PREFIX']+r"\Library\bin\graphviz"

This is a dirty little hack, but there are some certain advantages:

  • PATH changes are in effect locally and until os module is reloaded
  • No need to change module scripts
  • No need to change PATH system-wide

I am using Python 3.7, Windows 10, Anaconda. Graphviz was installed using conda install python-graphviz, but I don't believe there's any difference in this case


  1. Find:C:\Users\zhangqianyuan\AppData\Local\Programs\Python\Python36\Lib\site-packages\pydotplus

  2. Open graphviz.py

  3. Find line 1925 - line 1972, find the function:

    def create(self, prog=None, format='ps'):
    
  4. In the function find:

    if prog not in self.progs:
        raise InvocationException(
            'GraphViz\'s executable "%s" not found' % prog)
    
    if not os.path.exists(self.progs[prog]) or \
            not os.path.isfile(self.progs[prog]):
        raise InvocationException(
            'GraphViz\'s executable "{}" is not'
            ' a file or doesn\'t exist'.format(self.progs[prog])
        )
    
  5. Between the two blocks add this(Your Graphviz's executable path):

      self.progs[prog] = "C:/Program Files (x86)/Graphviz2.38/bin/gvedit.exe"`
    
  6. After adding the result is:

    if prog not in self.progs:
        raise InvocationException(
            'GraphViz\'s executable "%s" not found' % prog)
    
    self.progs[prog] = "C:/Program Files (x86)/Graphviz2.38/bin/gvedit.exe"
    
    if not os.path.exists(self.progs[prog]) or \
            not os.path.isfile(self.progs[prog]):
        raise InvocationException(
            'GraphViz\'s executable "{}" is not'
            ' a file or doesn\'t exist'.format(self.progs[prog])
        )
    
  7. save the changed file then you can run it successfully.

  8. you'd better save it as bmp file because png file will not work. picture is here


I got the same issue, so that I just installed pydotplus independently(pip3 install pydotplus) and import pydotplus, everything work fine.


If you have Anaconda, you could use Conda manager.

Type Conda at Start Panel and try install via Conda.

For example:

pip3 install graphviz

See: Graphviz's executables are not found (Python 3.4) and graphviz package doesn't add executable to PATH on windows #1666 and Problem with graphviz #1357 - it's a reoccurring problem (for that program) with the PATH environment variable settings. Installing particular versions, or in a particular order, or manually adding a PATH fixes the problem.

It's best if the Package sets the PATH correctly and removes it when you uninstall the Package (so you don't get too long a PATH - which usually won't happen). Setting it manually prevents future breakage and forces it to work, but you need to manually remove the extra text if you uninstall the Package.

Here's the advice from those three links:

  1. pip install graphviz
  2. conda install graphviz

or

You need to run

conda install python-graphviz

instead of

pip install graphviz

to get these bindings, which also work with conda's Graphviz package.

or

  1. Download and install graphviz-2.38.msi (use the newest version) from https://graphviz.gitlab.io/_pages/Download/Download_windows.html

  2. Set the path variable

        (a) Control Panel > System and Security > System > Advanced System Settings > Environment Variables > Path > Edit

        (b) add 'C:\Program Files (x86)\Graphviz2.38\bin'

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.