windows python3库pydot运行出现:FileNotFoundError: [WinError 2] "dot" not found in path.---完美解决
测试代码
#代码可以直接复制运行
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.externals.six import StringIO
import pydot
#import StringIO
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
dot_data = StringIO(initial_value=’’, newline=’\n’)
tree.export_graphviz(clf, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_dot(‘iris_simple.dot’)
graph[0].write_png(‘iris_simple.png’)
若报错如下(“dot” not found in path):
Traceback (most recent call last):
File “C:\Users\78754\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\pydot.py”, line 1914, in create
working_dir=tmp_dir,
File “C:\Users\78754\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\pydot.py”, line 136, in call_graphviz
**kwargs
File “C:\Users\78754\AppData\Local\conda\conda\envs\tensorflow\lib\subprocess.py”, line 676, in init
restore_signals, start_new_session)
File “C:\Users\78754\AppData\Local\conda\conda\envs\tensorflow\lib\subprocess.py”, line 957, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “D:\workspace python\ssd_py_server\src\test.py”, line 437, in
DTtest()
File “D:\workspace python\ssd_py_server\src\test.py”, line 435, in DTtest
graph[0].write_png(‘iris_simple.png’)
File “C:\Users\78754\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\pydot.py”, line 1734, in new_method
encoding=encoding)
File "C:\Users\78754\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\pydot.py", line 1817, in write
s = self.create(prog, format, encoding=encoding)
File “C:\Users\78754\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\pydot.py”, line 1921, in create
raise OSError(*args)
FileNotFoundError: [WinError 2] “dot” not found in path.
解决方法:
- 确认在Graphviz官网下载并安装了对应操作系统(Windows,Linux,Mac)的Graphviz软件。官网地址:https://www.graphviz.org/。直接百度也可找到。安装过程中有一个选项,建议选择添加至所有人。
- 将软件安装目录的bin文件添加到系统环境中。如我的电脑是Windows系统,Graphviz安装在D:\Program Files (x86)\Graphviz2.38,该目录下有bin文件,将该路径添加到系统环境变量中,即D:\Program Files (x86)\Graphviz2.38\bin。
- 使用pip命令安装以下模块:
pip install pydot-ng
pip install graphviz
pip install pydot - 安装完成后打开cmd,输入dot.exe是否报错找不到命令,若不报错则说明dot.exe可以正常调用,按回车后正确结果如下:
依照上述步骤基本不会有问题,若仍报错"dot" not found in path,而不是"dot.exe"程序,那么在提示目录下(如前文报错内容加粗目录)找到pydot.py文件的源码,在pydot.py中找到类Dot的定义,如下(截图已经修改):
如红色标记部分,原pydot.py中类Dot的成员变量self.prog = ‘dot’,因为我的程序还报错,所以上图就是修改结果,即self.prog = ‘dot.exe’,再次测试不会报错。
欢迎各位小伙伴评论
参考博主:此处为参考博主链接,感谢