解决 Windows OSError: pydot failed to call GraphViz.Please install GraphViz 报错

╰+哭是因爲堅強的太久メ 2023-02-10 05:10 227阅读 0赞

Windows操作系统下,运行pydot相关程序时(我的是keras.utils.plot_model)报错,提示没有安装GraphViz,事实上并不都是因为GraphViz没有安装,本文记录错误解决方法。

问题复现

操作系统:Win10

keras版本:2.2.4

在Win10系统下(Windows系列都可能出这个问题)keras建立简单的模型,执行 plot_model,报错:

  1. import keras
  2. from keras.models import Model
  3. from keras.layers import Input
  4. from keras.layers import Conv2D
  5. from keras.layers import GlobalAveragePooling2D
  6. from keras.layers import Dense
  7. import numpy as np
  8. from keras.utils import plot_model
  9. import os
  10. os.environ["PATH"] += os.pathsep + r'E:\Program Files (x86)\Graphviz2.38\bin'
  11. A = Input(shape=(16,16,3))
  12. x = Conv2D(filters=10, kernel_size=(3,3), padding='same', activation='relu')(A)
  13. x = Conv2D(filters=10, kernel_size=(3,3), padding='same', activation='relu')(x)
  14. x = GlobalAveragePooling2D()(x)
  15. x = Dense(units = 5, activation='softmax')(x)
  16. model = Model(A,x)
  17. model.summary()
  18. test_input = np.random.rand(1,16,16,3)
  19. results = model.predict(test_input)
  20. plot_model(model)

错误信息:

  1. builtins.OSError: `pydot` failed to call GraphViz.Please install GraphViz (https://www.graphviz.org/) and ensure that its executables are in the $PATH.

问题原因与解决方案

情况 1

  • 原因 :真的没有安装GraphViz
  • 解决方案:

    • 安装相应模块

    pip install pydot-ng
    pip install graphviz
    pip install pydot

如果问题没有排除,可能是GraphViz程序没有加入到系统路径,考虑情况2

情况 2

  • 原因:GraphViz程序没有加入到系统路径
  • 解决方案:

    • 下载graphviz-2.38.msi ,我是在这里下载的 https://www.5down.net/soft/graphviz.html
    • 我安装在了E盘:E:\Program Files (x86)\Graphviz2.38\bin
    • 将路径加入到系统变量
      format_png

目前为止是网上大多数存在的解决方案,相信大部分的同学到此为止已经解决了问题。

如果错误继续,那么我和你一样,进入情况3。

情况 3

  • 原因:依赖模块已经安装、程序已经加入系统变量,仍然出现上述提示,是因为pydot在建立Dot类时查找的dot程序的名字是 ’dot‘ 而不是我们 Windows 里的可执行程序文件名 ‘dot.exe’
  • 解决方案:改过来就好了,具体方法如下

    • 在报错的位置找到pydot

    format_png 1

    • 找到Dot类

    format_png 2

    • 初始部分代码是这样的:
    • class Dot(Graph):
      1. """A container for handling a dot language file. This class implements methods to write and process a dot language file. It is a derived class of the base class 'Graph'. """
    • 找到其中的 self.prog = 'dot'
    • 讲这句话替换为:
    • import platform

      1. system = platform.system()
      2. if system == 'Windows':
      3. self.prog = 'dot.exe'
      4. else:
      5. self.prog = 'dot'
    • 保存再次运行程序即可

    format_png 3

发表评论

表情:
评论列表 (有 0 条评论,227人围观)

还没有评论,来说两句吧...

相关阅读