FileNotFoundError: [WinError 2] "dot" not found in path.

1.环境:Windows

2.利用pydot库和以下代码将dot文件转换为png文件时报错

(graph,) = pydot.graph_from_dot_file('tree.dot',encoding="utf-8")
# Write graph to a png file
graph.write_png('tree.png')

Traceback (most recent call last):
  File "D:\Anaconda\lib\site-packages\pydot.py", line 1915, in create
    working_dir=tmp_dir,
  File "D:\Anaconda\lib\site-packages\pydot.py", line 136, in call_graphviz
    **kwargs
  File "D:\Anaconda\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "D:\Anaconda\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "E:\Pycharm\PyCharm 2019.2.1\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "E:\Pycharm\PyCharm 2019.2.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Administrator/Desktop/码云代码/BigData/机器学习/11_决策树.py", line 72, in <module>
    decision()
  File "C:/Users/Administrator/Desktop/码云代码/BigData/机器学习/11_决策树.py", line 67, in decision
    graph.write_png('tree.png')
  File "D:\Anaconda\lib\site-packages\pydot.py", line 1734, in new_method
    encoding=encoding)
  File "D:\Anaconda\lib\site-packages\pydot.py", line 1817, in write
    s = self.create(prog, format, encoding=encoding)
  File "D:\Anaconda\lib\site-packages\pydot.py", line 1922, in create
    raise OSError(*args)
FileNotFoundError: [WinError 2] "dot" not found in path.

3.解决方法:

3.1 下载 graphviz-2.38.msi 直接安装即可

3.2  在python安装环境下找到pydot.py文件,若是虚拟环境或者Anaconda环境的话直接到相应的python中查找即可

                                

用编辑器打开,找到 self.prog = 'dot'                

将其改为  

                       

再次运行代码,不报错了,生成相应的png图片

                                                         

发布了128 篇原创文章 · 获赞 95 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/qq_36853469/article/details/103555094