python 中解决 Graphviz Executables Are Not Found 错误
Graphviz 是一个包含开源工具的软件包,可帮助绘制图表,可在 Linux、Windows 和 macOS 上使用。
在 Python 中,我们可以使用 Graphviz 可执行文件以及包来创建图形。 然而,人们在使用它时常常会因为不同的原因而遇到问题。
本文介绍了如何解决运行 Python 脚本时未找到 Graphviz 可执行文件的错误。
安装Graphviz解决Python中 Graphviz executables are not found 错误
在向您展示如何解决该错误之前,让我们编写一段代码,使用 Graphviz Python 包创建一个简单的图形(假设您已经安装了 Graphviz python 包)。
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')
print(dot.source)
dot.render('doctest-output/round-table.gv', view=True)
上面代码的输出:
// The Round Table
digraph {
A [label="King Arthur"]
B [label="Sir Bedevere the Wise"]
L [label="Sir Lancelot the Brave"]
A -> B
A -> L
B -> L [constraint=false]
}
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\graphviz\backend\execute.py", line 81, in run_check
proc = subprocess.run(cmd, **kwargs)
File "C:\Python310\lib\subprocess.py", line 501, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Python310\lib\subprocess.py", line 969, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Python310\lib\subprocess.py", line 1438, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
上述异常是导致以下异常的直接原因:
Traceback (most recent call last):
File "c:\Users\akinl\Documents\HTML\python\graphz.py", line 11, in <module>
dot.render('doctest-output/round-table.gv', view=True)
File "C:\Python310\lib\site-packages\graphviz\_tools.py", line 171, in wrapper
return func(*args, **kwargs)
File "C:\Python310\lib\site-packages\graphviz\rendering.py", line 122, in render
rendered = self._render(*args, **kwargs)
File "C:\Python310\lib\site-packages\graphviz\_tools.py", line 171, in wrapper
return func(*args, **kwargs)
File "C:\Python310\lib\site-packages\graphviz\backend\rendering.py", line 324, in render
execute.run_check(cmd,
File "C:\Python310\lib\site-packages\graphviz\backend\execute.py", line 84, in run_check
raise ExecutableNotFound(cmd) from e
graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath('dot'), make sure the Graphviz executables are on your systems' PATH
错误消息的重要部分如下:
graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath('dot'), make sure the Graphviz executables are on your systems' PATH
问题是 Graphviz 可执行文件对于 Python 代码不可用,并且不存在于 PATH 变量中。 PATH 变量允许我们从软件可执行目录之外的任何目录访问软件。
因此,如果您没有 Graphviz,则必须安装它。 如果已安装,则必须将其添加到系统的 PATH 中。
因此,我们需要两件事才能成功运行 Graphviz 代码。
- 类库
- 软件本身
我们已经通过 pip 拥有了该库:
pip install graphviz
要安装 Graphviz,请转到其下载页面,然后按照每个操作系统的下载说明进行操作。 对于 Linux 操作系统(例如 Ubuntu、Fedora),您可以使用以下命令:
sudo apt install graphviz
sudo yum install graphviz
对于 macOS,您可以使用以下命令。
sudo port install graphviz
但是,如果您安装了 Homebrew,则可以使用:
brew install graphviz
有不同的方法和过程可以通过 Chocolatey、Windows 包管理器、Cygwin 或典型的 Windows 包为 Windows 安装它。 这里,我们将使用典型的安装包(64位版本5.0.0),大约大约4.728 MB。
-
打开安装文件并同意许可协议。
-
将 Graphviz 添加到系统 PATH,这对于使您的代码正常运行非常重要。 根据您的工作环境,您可以为所有用户或当前用户添加系统路径。
下图为所有用户选择。
-
选择安装位置并安装。
要验证 Graphviz 是否在我们的环境变量中,我们可以按照以下步骤操作:
-
搜索环境变量。
-
转到突出显示的按钮环境变量。
-
转到具有变量名称 Path 的突出显示行并双击它。
-
如果您最近安装了 Graphviz,则应使用最后的安装路径(突出显示),如下图所示
现在我们已经按照错误消息的子部分的建议验证了 Graphviz 位于我们的系统路径中,请确保 Graphviz 可执行文件位于系统的 PATH 上,关闭 IDE 并重新运行基于 Graphviz 的代码。
我们之前运行的导致错误的代码现在应该呈现并导出 PDF。
并且您的输出应该没有任何错误。
// The Round Table
digraph {
A [label="King Arthur"]
B [label="Sir Bedevere the Wise"]
L [label="Sir Lancelot the Brave"]
A -> B
A -> L
B -> L [constraint=false]
}
相关文章
Python 中错误 Address Already in Use
发布时间:2023/07/09 浏览次数:173 分类:Python
-
我们将通过示例介绍Python中何时出现 Address already in use 错误以及如何解决。Python 中的错误Address already in use 本文将讲述运行使用端口的程序时发生的Python堆栈错误。
Python 中错误 ValueError: Math Domain Error
发布时间:2023/07/09 浏览次数:607 分类:Python
-
在本篇文章中,我们的目标是探索解决 Python 中的 ValueError: math domain error 错误的不同方法。当编码方面数学(基础或高级)的使用存在固有缺陷时,Python 中通常会引发 ValueError: math domain error 错
Python 错误 Name xrange Is Not Defined
发布时间:2023/07/09 浏览次数:153 分类:Python
-
本篇文章将介绍如何解决 Python 中 name 'xrange' is not defined 的错误。解决Python中name 'xrange' is not defined错误 让我们尝试理解为什么会发生这个特定的错误。 让我们首先尝试复制这个问题。
Python 错误 TypeError: List Indices Must Be Integers, Not STR
发布时间:2023/07/09 浏览次数:954 分类:Python
-
在本篇文章中,我们的目标是探索如何避免 TypeError: list indices must be integers or slices, not str。TypeError主要发生在Python中,每当操作的数据类型出现问题时。
Python 中错误 AttributeError: __Enter__
发布时间:2023/07/09 浏览次数:2241 分类:Python
-
在 Python 中,AttributeError 是在未定义 __enter__ 函数的情况下通过 with 语句使用类的对象时导致的错误。
Python 错误 ModuleNotFoundError: No Module Named '_Ctypes'
发布时间:2023/07/09 浏览次数:686 分类:Python
-
本篇文章旨在了解如何解决 Python 中的 ModuleNotFoundError: No module named '_ctypes'。了解Python中 ModuleNotFoundError: No module named '_ctypes' 根本原因
Python 错误 AttributeError: '_io.TextIOWrapper' Object Has No Attribute 'Split'
发布时间:2023/07/09 浏览次数:1063 分类:Python
-
本篇文章将介绍如何修复 Python 中的 AttributeError: '_io.TextIOWrapper' object has no attribute 'split'。在 _io.TextIOWrapper 上使用 split() 方法会返回 AttributeError
Python 错误 AttributeError: _csv.reader Object Has No Attribute Next
发布时间:2023/07/09 浏览次数:286 分类:Python
-
本篇文章将介绍如何修复 Python 中的 AttributeError: '_csv.reader' object has no attribute 'next'。修复 Python 中的 AttributeError: '_csv.reader' object has no attribute 'next' 错误
Python 错误 Error: Invalid Command Bdist_wheel
发布时间:2023/07/09 浏览次数:847 分类:Python
-
在 Python 中构建 wheel 时,有时 setup.py 可能会退出并出现错误 invalid command 'bdist_wheel'。 本篇文章将讨论在 Python 中解决此问题的可能解决方案。安装wheel包来修复Python中 Error:invalid command 'bdist_