未找到 GraphViz 的可执行文件 - 为啥通过 pip 安装 graphViz 后没有安装可执行文件?

Posted

技术标签:

【中文标题】未找到 GraphViz 的可执行文件 - 为啥通过 pip 安装 graphViz 后没有安装可执行文件?【英文标题】:GraphViz's executables not found - Why are there no executables installed after installation of graphViz via pip?未找到 GraphViz 的可执行文件 - 为什么通过 pip 安装 graphViz 后没有安装可执行文件? 【发布时间】:2019-10-04 15:39:48 【问题描述】:

我在虚拟环境(Windows 8.1)中安装了 pydotplus 和 graphviz。 现在我想可视化决策树。但是 pydotplus 无法找到 GraphViz 的可执行文件。

from sklearn import tree
from sklearn.datasets import load_iris
import pydotplus
from IPython.display import Image

iris = load_iris()
X,y = iris.data[:,2:], iris.target

clf = tree.DecisionTreeClassifier(max_depth=2)
clf.fit(X,y)
dot_data = tree.export_graphviz(clf,
                     out_file=None,
                     feature_names=iris.feature_names[2:],
                     class_names=iris.target_names,
                     rounded=True,
                     filled=True)


graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())

人们通过在 PATH 中添加 GraphViz bin 目录来解决这个问题。显然这个目录通常是C:\Program Files (x86)\Graphviz2.34\bin\。然而,这不是我的情况。我怎样才能找到它?

【问题讨论】:

使用 windows 搜索来查找 graphviz 或 dot.exe?顺便说一句,你已经安装了 pydotplus,但你也安装了 graphviz 吗? 是的,我已经安装了 graphviz 和 pydotplus。我在我的完整硬盘驱动器中搜索“graphviz.exe”,没有命中(也在使用 pip 重新安装/更新 graphviz 之后)。我仍然收到异常:“pydotplus.graphviz.InvocationException:未找到 GraphViz 的可执行文件”。有什么想法吗? 【参考方案1】:

据我从 cmets 了解到,您已经使用 pip 安装了 graphviz。问题是,名为graphviz in pip 的包只是graphviz 应用程序的python 接口。换句话说,它类似于您尝试使用的 pydotplus 包。

这些包的作用是为您提供一些类和方法,让您在 Python 代码中乱七八糟,当需要渲染图形时,它们只需调用 graphviz 二进制文件并将生成的点源代码发送给它。当然,要让它们工作,你必须在你的机器上安装上面提到的 graphviz 二进制文件。

你需要做的是下载并运行graphviz installer(Windows的链接),它与python和pip没有任何连接。安装后,您将在 Program Files 中获得 Graphviz 文件夹,其中包含 graphviz 可执行文件。

在使用 pydotplus 之前,您可能需要将此文件夹添加到 PATH。

要检查一切是否已设置,请运行以下命令:

> dot -?

您应该看到点命令手册页。

【讨论】:

非常感谢。正如您所说,仅通过 pip 安装侧包是不够的。奇怪的是,通过 conda 安装时,可执行文件(...\Anaconda3\Library\bin\graphviz)和站点包(...\Anaconda3\Lib\site-packages\graphviz)一起自动安装,而通过 pip 安装时,只安装站点包。

以上是关于未找到 GraphViz 的可执行文件 - 为啥通过 pip 安装 graphViz 后没有安装可执行文件?的主要内容,如果未能解决你的问题,请参考以下文章

InvocationException:未找到 GraphViz 的可执行文件

pydot.InvocationException:未找到 GraphViz 的可执行文件

为啥在查看证书详细信息之前,数字签名的可执行文件会被视为未签名

未找到 xcode 13 可执行文件

为啥 AssocQueryString 找不到与图像扩展关联的可执行文件?

由于调试符号,巨大的可执行文件,为啥?