找不到 GraphViz 的可执行文件 - 为什么在通过 pip 安装 graphViz 后没有安装可执行文件?

GraphViz's executables not found - Why are there no executables installed after installation of graphViz via pip?

我在虚拟环境中安装了 pydotplus 和 graphviz (Windows 8.1)。 现在我想可视化决策树。但是 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())

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

正如我从评论中了解到的那样,您已经使用 pip 安装了 graphviz。问题是,名为 graphviz in pip 的包只是 graphviz 应用程序的 python 接口。换句话说,它类似于您尝试运行的 pydotplus 包。

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

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

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

要检查是否一切都已设置,运行此命令:

> dot -?

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