Search code examples
pythonnetworkxgraphviz

Graphviz throws errors calling view() function


I tried to rebuild the code of fsm.py to display a process sequence for a production line. I did this in an jupyter notebook and also run the command

conda install -c anaconda graphviz

This install was successfully completed without errors.

The given code from the source is:

from graphviz import Digraph

f = Digraph('finite_state_machine', filename='fsm.gv')
f.attr(rankdir='LR', size='8,5')

f.attr('node', shape='doublecircle')
f.node('LR_0')
f.node('LR_3')
f.node('LR_4')
f.node('LR_8')

f.attr('node', shape='circle')
f.edge('LR_0', 'LR_2', label='SS(B)')
f.edge('LR_0', 'LR_1', label='SS(S)')
f.edge('LR_1', 'LR_3', label='S($end)')
f.edge('LR_2', 'LR_6', label='SS(b)')
f.edge('LR_2', 'LR_5', label='SS(a)')
f.edge('LR_2', 'LR_4', label='S(A)')
f.edge('LR_5', 'LR_7', label='S(b)')
f.edge('LR_5', 'LR_5', label='S(a)')
f.edge('LR_6', 'LR_6', label='S(b)')
f.edge('LR_6', 'LR_5', label='S(a)')
f.edge('LR_7', 'LR_8', label='S(b)')
f.edge('LR_7', 'LR_5', label='S(a)')
f.edge('LR_8', 'LR_6', label='S(b)')
f.edge('LR_8', 'LR_5', label='S(a)')

f.view()

From the source it is supposed to give this output:

Code Output

But, when I run the code I get this pile of errors:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
~\Anaconda3\lib\site-packages\graphviz\backend.py in run(cmd, input, capture_output, check, quiet, **kwargs)
    146     try:
--> 147         proc = subprocess.Popen(cmd, startupinfo=get_startupinfo(), **kwargs)
    148     except OSError as e:

~\Anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    708                                 errread, errwrite,
--> 709                                 restore_signals, start_new_session)
    710         except:

~\Anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
    996                                          os.fspath(cwd) if cwd is not None else None,
--> 997                                          startupinfo)
    998             finally:

FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

During handling of the above exception, another exception occurred:

ExecutableNotFound                        Traceback (most recent call last)
<ipython-input-24-a4382ae6bf37> in <module>()
     10 g1.edge('A', 'B')
     11 
---> 12 g1.view()

~\Anaconda3\lib\site-packages\graphviz\files.py in view(self, filename, directory, cleanup)
    213         """
    214         return self.render(filename=filename, directory=directory, view=True,
--> 215                            cleanup=cleanup)
    216 
    217     def _view(self, filepath, format):

~\Anaconda3\lib\site-packages\graphviz\files.py in render(self, filename, directory, view, cleanup, format, renderer, formatter)
    186             format = self._format
    187 
--> 188         rendered = backend.render(self._engine, format, filepath, renderer, formatter)
    189 
    190         if cleanup:

~\Anaconda3\lib\site-packages\graphviz\backend.py in render(engine, format, filepath, renderer, formatter, quiet)
    181     """
    182     cmd, rendered = command(engine, format, filepath, renderer, formatter)
--> 183     run(cmd, capture_output=True, check=True, quiet=quiet)
    184     return rendered
    185 

~\Anaconda3\lib\site-packages\graphviz\backend.py in run(cmd, input, capture_output, check, quiet, **kwargs)
    148     except OSError as e:
    149         if e.errno == errno.ENOENT:
--> 150             raise ExecutableNotFound(cmd)
    151         else:  # pragma: no cover
    152             raise

ExecutableNotFound: failed to execute ['dot', '-Tpng', '-O', 'Graph.gv'], make sure the Graphviz executables are on your systems' PATH

Didn't I installed graphviz correctly? That is what I understand from the errors. If so, what could be the right way to install it?

Is there even a potential way to create such a graph with networkx? I also tried it, but failed to create such a sequenced presentation of the nodes.

Thank you really much for your help, I'm sitting on this since ours.


Solution

  • From this thread I found the information to change the install promt for conda to:

    conda install python-graphviz
    

    This cleared all my problems!Thank you @Blckknght for the important information to install it on the local machine as well and not only within conda. I hope other users with the same struggle will find help in our elaboration.