python - Windows 10 Anaconda Graphviz 错误无法工作

标签 python anaconda graphviz

一切都运行良好,直到“graph.render(“iris”)”行。然后我收到此错误:ExecutableNotFound:无法执行 ['dot', '-Tpdf', '-O', 'iris'],请确保 Graphviz 可执行文件位于系统的 PATH 上

graph.render("iris")
Traceback (most recent call last):

File "<ipython-input-13-c002c0ff127c>", line 1, in <module>
graph.render("iris")

File "C:\Users\Rafay\Anaconda3\lib\site-packages\graphviz\files.py", line 
175, in render
rendered = backend.render(self._engine, self._format, filepath)

File "C:\Users\Rafay\Anaconda3\lib\site-packages\graphviz\backend.py", line 
127, in render
raise ExecutableNotFound(args)

ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'iris'], make 
sure the Graphviz executables are on your systems' PATH

我有 Windows 10,并且正在使用 Anaconda 和 spy 程序。完整代码如下:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import cross_val_score
import graphviz
from util import visualize_iris


# load iris petal data set
iris = load_iris()

X = iris.data
y = iris.target



# create Decision Tree Classifier object
dt = DecisionTreeClassifier()

# fit Decision Tree to Data
dt = dt.fit(iris.data, iris.target)

# look at the importance of each feature
print(dt.feature_importances_)
print(dt.score(X, y))

# visualize decision tree
# tree visualization
dot_data = export_graphviz(dt, out_file=None,
                                feature_names=iris.feature_names,
                                class_names=iris.target_names,
                                filled=True, rounded=True,
                                special_characters=True
                                )

graph = graphviz.Source(dot_data)
graph.render("iris")

同样,一切都运行良好,直到“graph.render(“iris”)”行。然后我收到上面的错误。

非常感谢大家,你们都很棒!

最佳答案

请注意,我使用的是 Windows 10。以下某些内容可能适用于其他版本的 Windows 或操作系统,也可能不适用于:

**注2:**
“系统上的 Graphviz bin 文件地址”可以是 C:\Program Files (x86)\Graphviz2.38\bin 或您安装 Graphviz 的任何其他路径。

我们不仅对 Graphviz 有问题,而且对我们想要在 Jupyter 中使用的其他外部 EXE 文件也有问题。
原因是当 jupyter 想要导入一个包时,它会在工作目录中查找它,当它找不到该包时,它会返回此类错误。
我们能做的就是解决这个问题,如下:
1) 检查您的系统上是否安装了 Graphviz,如果没有,您可以从以下位置下载并安装它:

https://graphviz.gitlab.io/_pages/Download/Download_windows.html
然后安装它。安装 Graphviz 时,请记住安装位置(哪个文件夹中)。 如果您在使用

时看到上述错误
import graphviz

那么你有几个选择:

2)您可以通过

调用ipynb中的.exe文件
import os
os.environ["PATH"] += os.pathsep + r'the Graphviz bin file address on your system'

根据我的经验,它仅适用于我正在使用的同一个 ipynb,每次打开笔记本时我都需要调用这行代码。

3)如果你想让Jupyter在哪里找到exe文件,你需要设置环境路径。
在 Windows 10 中,您可以执行以下操作:
控制面板>系统和安全>系统>高级系统设置>环境变量>路径>编辑>新建
然后添加“您系统上的 Graphviz bin 文件地址” 在 Windows 8 或更低版本中,请转至:
控制面板 > 系统和安全 > 系统 > 高级系统设置 > 环境变量
然后将;(分号)+“系统上的Graphviz bin文件地址”添加到路径字符串的末尾
注意:记得重新启动机器。

4)即使这不起作用,定义一个变量:
控制面板 > 系统和安全 > 系统 > 高级系统设置 > 环境变量,然后:

Start to define an environmental variable

然后定义一个变量如下: Remember to name the variable Graphviz

记住将变量命名为 Graphviz。最后重新启动你的电脑并希望它能正常工作。

关于python - Windows 10 Anaconda Graphviz 错误无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48736428/

相关文章:

python - 如何在从智能手机接收蓝牙数据的同时使用键盘移动 Pygame 的多边形?

python - jupyter lab 404错误如何解决

python - Anaconda 无法识别 : xgboost, catboost 和 lightgbm

graphviz - 防止 Graphviz 箭头边缘穿过邻居节点

scikit-learn - 绘制决策树,graphvizm pydotplus

Graphviz 边缘不可辨别/边缘标签被覆盖

Python 漂亮表 : Add title above the table's header

python - 使用python解析C代码

python - 如何在 beautifulsoup findAll 的类之间使用 OR 运算符?

path - 如何更改 Jupyter 启动文件夹