Graphviz引发错误调用view()函数

时间:2018-11-17 00:18:01

标签: python networkx graphviz

我试图重建fsm.py的代码以显示生产线的流程顺序。我是在jupyter笔记本中完成此操作的,还运行了命令

conda install -c anaconda graphviz

此安装成功完成,没有错误。

源代码中给出的代码是:

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

从源头来看,应该提供以下输出:

Code Output

但是,当我运行代码时,出现了很多错误:

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

我没有正确安装graphviz吗?我从错误中了解到这一点。如果是这样,什么是正确的安装方式?

是否有可能使用networkx创建这样的图?我也尝试过,但是未能创建节点的顺序显示。

非常感谢您的帮助,自我们以来我就一直坐在这里。

2 个答案:

答案 0 :(得分:2)

graphviz python模块不包含用于查看其创建的图形的程序。 the documentation中提到了这一点:

  

要呈现生成的DOT源代码,您还需要安装Graphviz(download page)。

答案 1 :(得分:1)

this thread中,我找到了将conda的安装程序更改为的信息:

.toBase64Image()

这清除了我所有的问题!感谢@Blckknght,您也需要将重要信息也安装到本地计算机上,而不仅仅是在conda中。希望其他同样努力的用户能在我们的阐述中有所帮助。