Installing Graphviz for use with Python 3 on Windows 10
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 Files\Python36\lib\site-packages\graphviz\backend.py", line 124, in render subprocess.check_call(args, startupinfo=STARTUPINFO, stderr=stderr) File "C:\Program Files\Python36\lib\subprocess.py", line 286, in check_call retcode = call(*popenargs, **kwargs) File "C:\Program Files\Python36\lib\subprocess.py", line 267, in call with Popen(*popenargs, **kwargs) as p: File "C:\Program Files\Python36\lib\subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "C:\Program Files\Python36\lib\subprocess.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:\foldername\testing.py", line 11, in dot.render('round-table.gv', view=True) File "C:\Program Files\Python36\lib\site-packages\graphviz\files.py", line 176, in render rendered = backend.render(self._engine, self._format, filepath) File "C:\Program Files\Python36\lib\site-packages\graphviz\backend.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?
How to Install Graphviz on Windows
This will install both the system library and the python library. However, if you are looking for the pip install, method, then follow the steps below:
- Go to the downloads page of Graphviz, and download the executable for Windows (depending on your PC type, 64-bit or 32-bit).
- Run the .exe file. You will be asked whether you want to add graphviz to the system PATH. Either select the option for ‘ALL Users’ or ‘Current User’.
- Complete the installation. This takes care of the graphviz system library
- For the graphviz python library, run the following pip command:
- Restart the IDE on which you wish to run Graphviz. If that doesn’t work, restart your PC and try again.
Note: If you just install the graphviz python library and not the system library, or if you don’t add the system library to the system PATH, you will get the following error on trying to run, say .visualize() on a dask dataframe:
ExecutableNotFound: failed to execute ['dot', '-Kdot', '-Tpng'], make sure the Graphviz executables are on your systems' PATH
For any more help related to graphviz installation, you can check out their forum post.
If after installing graphviz, you wish to customize the task graphs generated on clicking .visualize() in dask, then read this post to see how you can do that.
Found this post helpful? Then check out further posts on Python on iotespresso.com. Also, follow IoT Espresso on Twitter to get notified about every new post.
graphviz 0.20.1
This package facilitates the creation and rendering of graph descriptions in the DOT language of the Graphviz graph drawing software (upstream repo) from Python.
Create a graph object, assemble the graph by adding nodes and edges, and retrieve its DOT source code string. Save the source code to a file and render it with the Graphviz installation of your system.
Use the view option/method to directly inspect the resulting (PDF, PNG, SVG, etc.) file with its default application. Graphs can also be rendered and displayed within Jupyter notebooks (formerly known as IPython notebooks, example, nbviewer) as well as the Jupyter QtConsole.
Links
- GitHub: https://github.com/xflr6/graphviz
- PyPI: https://pypi.org/project/graphviz/
- Documentation: https://graphviz.readthedocs.io
- Changelog: https://graphviz.readthedocs.io/en/latest/changelog.html
- Issue Tracker: https://github.com/xflr6/graphviz/issues
- Download: https://pypi.org/project/graphviz/#files
Installation
This package runs under Python 3.7+, use pip to install:
Make sure that the directory containing the dot executable is on your systems’ PATH (sometimes done by the installer; setting PATH on Linux, Mac, and Windows).
Quickstart
Check the generated source code:
Save and render the source code:
Save and render and view the result:
Caveat: Backslash-escapes and strings of the form have a special meaning in the DOT language. If you need to render arbitrary strings (e.g. from user input), check the details in the user guide.
See also
- pygraphviz – full-blown interface wrapping the Graphviz C library with SWIG
- graphviz-python – official Python bindings (documentation)
- pydot – stable pure-Python approach, requires pyparsing
License
This package is distributed under the MIT license.