windows python3库pydot运行出现:FileNotFoundError: [WinError 2] "dot" not found in path.

测试代码:

    from sklearn.datasets import load_iris
    from sklearn import tree
    iris = load_iris()
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(iris.data, iris.target)
    from sklearn.externals.six import StringIO
    import pydot
    dot_data = StringIO() 
    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')

报错:

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-2.38.msi

配置环境变量:

windows python3库pydot运行出现:FileNotFoundError: [WinError 2]

测试之后还是报错,后来发现,错误提示是找不到“dot”程序,而不是"dot.exe"程序,这就好办了,找到pydot的源码,在pydot.py中找到类Dot
 

windows python3库pydot运行出现:FileNotFoundError: [WinError 2]

 

修改self.prog = 'dot'为self.prog = 'dot.exe',之后测试,成功运行例子

你可能感兴趣的:(python,报错)