# 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


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

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.

• Thank you so much for asking this question. – AturSams May 16 '19 at 14:23
• The best way of dealing with these problematic installation issues probably by using some kind of independent environment like pipenv, virtualenv or Docker Containers, though I haven't properly checked it yet. – Philippe Fanaro Aug 24 '19 at 2:51

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.

1. pip install graphviz
2. conda install graphviz

or

You need to run

conda install python-graphviz

pip install graphviz

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

or

2. Set the path variable

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

• That's exactly what I described I did in the post. And the other links you mentioned I had also already seen or tried. They are also getting quite old so I thought maybe someone had come up with a better solution in the mean time. – Philippe Fanaro Aug 25 '18 at 22:24
• I think I'll try to, maybe tomorrow, to uninstall everything I have related to python and see what happens, maybe a clean sheet would help, idk. – Philippe Fanaro Aug 25 '18 at 22:26
• I still can't run GraphViz after testing all the above methods – Revolucion for Monica Feb 7 at 10:15
• @IggyPass, this answer is almost a couple of years old. I checked the 2nd link above and that BR has been closed; a fix is offered. The third link suggests the third solution I presented, apparently that's still the newest 3rd party compiled version offered; the newest versions are only offered in source and you need to compile them yourself. I'm using a mobile phone while I wait to buy a new computer, unfortunately I won't be able to update this answer until then; the pre-compiled version and most of these answers share a common theme, you need to set the PATH appropriately for your system. – Rob Feb 7 at 10:32

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

If you have Anaconda, you could use Conda manager.

Type Conda at Start Panel and try install via Conda.

For example:

pip3 install graphviz

• (...) I've already tried this... – Philippe Fanaro Aug 25 '18 at 22:12
• But via Conda manager? – fuwiak Aug 25 '18 at 22:26
• Yes, I did, and in several different orders. – Philippe Fanaro Aug 26 '18 at 0:17

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.

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(

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])
)


  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(

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.

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 :)

Updating through running in the anaconda prompt:

conda install python-graphviz


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

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

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