How to use Graphviz with Anaconda/Spyder?

36,434

Solution 1

Here are the steps that worked for me. Much of this information was available but spread out in several different StackOverflow posts and other websites. I hope that this serves as a good one-stop resource.

  1. Go to the Graphviz website and download and install to your computer (do NOT need to install for all users).

  2. Download and install Anaconda3.5 from the Continuum website.

  3. Add Graphviz to the environment variable "Path":

    • Go to Computer > Properties > Advanced system settings > Environment Variables and then find "Path" in the system variables box. Click on Path and click edit.
    • Append ;C:\Program Files (x86)\Graphviz2.38\bin to the end of the many paths that are already present in Path. Note, the path to Graphviz may be different for you so make sure to put the correct path. The folder "bin" should have many files including the dot.exe application.
    • To check the install go to the command prompt and enter: dot -V this should return the version of Graphviz installed. For example, dot - graphviz version 2.38.0. If this does not work, enter set and look for the Graphviz path.
  4. Go to the Anaconda command prompt and enter: pip install graphviz

  5. Restart Spyder or launch it if not already open.

  6. Now within your Python script add import graphviz

  7. Below is an example of how to create a graph and render it using Graphviz from a Graphviz tutorial

    import graphviz
    
    dot = graphviz.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.edges(['AB', 'AL'])
    dot.edge('B', 'L', constraint='false')
    
    dot.render('FileName', view=True)
    
  8. Below is an example of how to create a graph from a pre-generated .gv file (at least a starting point for exploration)

    from graphviz import Source
    Source.from_file('file.gv')
    

Usefull Links:

Getting started with Graphviz and Python

Graphviz Attributes

Dot Guide

Another StackOverflow Question

Versions Used:

Anaconda 3.5 (comes with Spyder)

Graphviz 2.38

Solution 2

I gave a bounty to the other question, but then realized there's a better way:

  • conda install graphviz installs the binaries for GraphViz, (So you don't need to visit GraphViz website, and they'll presumably be kept updated in the usual conda way.)
  • conda install python-graphviz installs the Python frontend for GraphViz. (This is the same as pip install graphviz, which has led to great confusion.)

The conda version of the graphviz frontend has been patched to support the binaries installed by the conda graphviz package, so for graphviz itself, this should be all you need.

For pydot, however, in Windows, this will not work until you include these binaries in your PATH. You can do this temporarily within your script with:

import os
os.environ["PATH"] += os.pathsep + 'PATH_STRING'

before the command that calls pydot.

The PATH_STRING is either C:\Anaconda3\envs\ENV_NAME\Library\bin\graphviz for a specific conda environment, or C:\Anaconda3\Library\bin\graphviz for the default environment.

(I had to install a py3.6 environment for Tensorflow, since it doesn't yet support py3.7 and conda install tensorflow was hanging for hours, trying to figure out how to downgrade every package on my system. (Probably other people installing GraphViz are doing so for the same reason.))

Solution 3

  1. Open Anaconda Prompt
  2. Run-> "conda install python-graphviz" in anaconda prompt.
  3. After install graphviz then copy the directory: C:\Users\Admin\anaconda3\Library\bin\graphviz
  4. Open Control Panel\System\Advanced system settings
    Enviornment variables\path\Edit\New
    Paste that copied directory and then click Ok
Share:
36,434
Scott G
Author by

Scott G

Updated on October 07, 2020

Comments

  • Scott G
    Scott G over 3 years

    I am attempting to use Graphviz from Spyder (via an Anaconda install). I am having trouble understanding what is needed to do this and how to go about loading packages, setting variables, etc.

    I straight forward approach for a new Python and Graphviz and Spyder user would be great!

    Also, apart from just creating and running Graphviz, how can one run Graphviz from python with a pre-generated .gv file?

  • endolith
    endolith over 5 years
    On Anaconda 5.3.1 and Windows, the binaries are already installed in C:\Anaconda3\envs\env_name\Library\bin\graphviz or C:\Anaconda3\Library\bin\graphviz (default env)
  • endolith
    endolith over 5 years
    Actually I think I was wrong. conda install graphviz installs the binaries as in my previous comment. pip install graphviz installs the python frontend for GraphViz. conda install python-graphviz installs the python frontend as well. So you don't need to visit GraphViz website if you do conda install graphviz python-graphviz.
  • Carlos Cordoba
    Carlos Cordoba over 5 years
    Then in Windows, you need to add the binaries to your PATH, this is asking for big trouble. Hence my downvote.
  • albert
    albert over 5 years
    @CarlosCordoba why should adding / appendix to the PATH be big trouble?
  • endolith
    endolith over 5 years
    @CarlosCordoba The binaries are not accessible to the python graphviz frontend unless they are in the PATH, so nothing works without it. You can add the path inside your python program before importing graphviz, but it's easier to just do it once.
  • endolith
    endolith over 5 years
    @CarlosCordoba Yet here you say that conda install python-graphviz is "patched to work with conda's Graphviz C package"? Maybe I'm misremembering, and it's conda install pydot that requires the PATH change? (This is all incredibly confusing.)
  • Carlos Cordoba
    Carlos Cordoba over 5 years
    @albert, in general it's not bad to add new directories to PATH, but the one that @endolith mentiones is particularly dangerous because it contains a lot of binaries and DLLs that can conflict with other Anaconda packages and lead to subtle bugs or segfaults.
  • Carlos Cordoba
    Carlos Cordoba over 5 years
    @endolith, yes, it's pydot the one that's broken. I opened this issue to ask Anaconda people to fix it.
  • endolith
    endolith over 5 years
    @CarlosCordoba So after conda install graphviz python-graphviz, import graphviz should work fine? Or has that fix not been published yet?
  • endolith
    endolith over 5 years
    @CarlosCordoba And Scott G's answer would have the same problems, no?
  • Carlos Cordoba
    Carlos Cordoba over 5 years
    "So after conda install graphviz python-graphviz, import graphviz should work fine?" Yes, I fixed things for python-graphviz before leaving Anaconda, but pydot came later.
  • Carlos Cordoba
    Carlos Cordoba over 5 years
    And yes, Scott G's answer has the same problem as yours.
  • Carlos Cordoba
    Carlos Cordoba over 5 years
    @endolith, thanks a lot! Let's hope Anaconda people won't take long to fix the pydot issue.