Installing Graphviz for use with Python 3 on Windows 10
Question:
So I’ve had Python 3.6 on my Windows 10 computer for a while now, and today I just downloaded and installed the graphviz 0.8.2
(https://pypi.python.org/pypi/graphviz) package via the admin commandline with:
pip3 install graphviz
It was only after this point that I downloaded the Graphviz 2.38 MSI installer file and installed the program at:
C:Program Files (x86)Graphviz2.38
So then I tried to run this simple Python program:
from graphviz import Digraph
dot = Digraph(comment="The round table")
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
dot.render('round-table.gv', view=True)
But unfortunately, I received the following error when I try to run my Python program from commandline:
Traceback (most recent call last):
File "C:Program FilesPython36libsite-packagesgraphvizbackend.py", line 124, in render
subprocess.check_call(args, startupinfo=STARTUPINFO, stderr=stderr)
File "C:Program FilesPython36libsubprocess.py", line 286, in check_call
retcode = call(*popenargs, **kwargs)
File "C:Program FilesPython36libsubprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "C:Program FilesPython36libsubprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:Program FilesPython36libsubprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:foldernametesting.py", line 11, in <module>
dot.render('round-table.gv', view=True)
File "C:Program FilesPython36libsite-packagesgraphvizfiles.py", line 176, in render
rendered = backend.render(self._engine, self._format, filepath)
File "C:Program FilesPython36libsite-packagesgraphvizbackend.py", line 127, in render
raise ExecutableNotFound(args)
graphviz.backend.ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'round-table.gv'], make sure the Graphviz executables are on your systems' PATH
Notice how what I’ve asked seems VERY similar to this question asked here:
"RuntimeError: Make sure the Graphviz executables are on your system's path" after installing Graphviz 2.38
But for some reason, adding those paths (suggested in the solutions at the link above) to the system variables isn’t working, and I don’t know why! I tried restarting the computer after adding the paths as well, still to no success. See the image below:
Although the other suggested solution, which was to add these few lines in front of my Python code, did work:
import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
But here’s the issue: I don’t understand why adding to the environment variables didn’t work, and this is my primary concern. So my question is this: why did adding those lines of code in front of the Python script work but changing the environment variables didn’t? What do I need to do to get my script to run without adding those lines of code in front?
Answers:
Can you please post the output you get when you type SET
in a cmd window after setting the PATH environment variable?
Does it contain C:/Program Files (x86)/Graphviz2.38/bin/
?
A cmd window must be restarted before updated environment variables become effective!
Also make sure to add to the SYSTEM path variable rather than current user path variable.
So I’ve had Python 3.6 on my Windows 10 computer for a while now, and today I just downloaded and installed the graphviz 0.8.2
(https://pypi.python.org/pypi/graphviz) package via the admin commandline with:
pip3 install graphviz
It was only after this point that I downloaded the Graphviz 2.38 MSI installer file and installed the program at:
C:Program Files (x86)Graphviz2.38
So then I tried to run this simple Python program:
from graphviz import Digraph
dot = Digraph(comment="The round table")
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
dot.render('round-table.gv', view=True)
But unfortunately, I received the following error when I try to run my Python program from commandline:
Traceback (most recent call last):
File "C:Program FilesPython36libsite-packagesgraphvizbackend.py", line 124, in render
subprocess.check_call(args, startupinfo=STARTUPINFO, stderr=stderr)
File "C:Program FilesPython36libsubprocess.py", line 286, in check_call
retcode = call(*popenargs, **kwargs)
File "C:Program FilesPython36libsubprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "C:Program FilesPython36libsubprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:Program FilesPython36libsubprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:foldernametesting.py", line 11, in <module>
dot.render('round-table.gv', view=True)
File "C:Program FilesPython36libsite-packagesgraphvizfiles.py", line 176, in render
rendered = backend.render(self._engine, self._format, filepath)
File "C:Program FilesPython36libsite-packagesgraphvizbackend.py", line 127, in render
raise ExecutableNotFound(args)
graphviz.backend.ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'round-table.gv'], make sure the Graphviz executables are on your systems' PATH
Notice how what I’ve asked seems VERY similar to this question asked here:
"RuntimeError: Make sure the Graphviz executables are on your system's path" after installing Graphviz 2.38
But for some reason, adding those paths (suggested in the solutions at the link above) to the system variables isn’t working, and I don’t know why! I tried restarting the computer after adding the paths as well, still to no success. See the image below:
Although the other suggested solution, which was to add these few lines in front of my Python code, did work:
import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
But here’s the issue: I don’t understand why adding to the environment variables didn’t work, and this is my primary concern. So my question is this: why did adding those lines of code in front of the Python script work but changing the environment variables didn’t? What do I need to do to get my script to run without adding those lines of code in front?
Can you please post the output you get when you type SET
in a cmd window after setting the PATH environment variable?
Does it contain C:/Program Files (x86)/Graphviz2.38/bin/
?
A cmd window must be restarted before updated environment variables become effective!
Also make sure to add to the SYSTEM path variable rather than current user path variable.