gpt4 book ai didi

python - Graphviz 抛出调用 view() 函数的错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:08:12 24 4
gpt4 key购买 nike

我试图重建 fsm.py 的代码显示生产线的工艺顺序。我在 jupyter notebook 中执行了此操作并运行了命令

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 创建这样的图?我也尝试过,但未能创建节点的这种顺序表示。

非常感谢您的帮助,自从我们开始以来,我就一直坐在这上面。

最佳答案

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

To render the generated DOT source code, you also need to install Graphviz (download page).

关于python - Graphviz 抛出调用 view() 函数的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53347010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com