【问题标题】:"ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work." However both are installed“ImportError: 无法导入 pydot。您必须安装 pydot 和 graphviz 才能使 `pydotprint` 工作。”但是两者都安装了
【发布时间】:2020-01-26 15:36:49
【问题描述】:

我已经安装了 pydotplus 和 graphviz(我运行 Windows 10 和 Python 3.6)。

但是,当我尝试绘制我的 Keras 模型时,我遇到了一个异常,我被要求安装 pytdot 和 graphviz。

keras.utils.plot_model(model, 'my_first_model.png')

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pydot.py in create(self, prog, format, encoding)
   1914                 arguments=arguments,
-> 1915                 working_dir=tmp_dir,
   1916             )

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pydot.py in call_graphviz(program, arguments, working_dir, **kwargs)
    135         stdout=subprocess.PIPE,
--> 136         **kwargs
    137     )

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    708                                 errread, errwrite,
--> 709                                 restore_signals, start_new_session)
    710         except:

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
    996                                          os.fspath(cwd) if cwd is not None else None,
--> 997                                          startupinfo)
    998             finally:

FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

FileNotFoundError                         Traceback (most recent call last)
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\utils\vis_utils.py in _check_pydot()
     44     # to check the pydot/graphviz installation.
---> 45     pydot.Dot.create(pydot.Dot())
     46   except Exception:

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pydot.py in create(self, prog, format, encoding)
   1921                     prog=prog)
-> 1922                 raise OSError(*args)
   1923             else:

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

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-58-f0e169c88003> in <module>()
----> 1 keras.utils.plot_model(model, 'my_first_model.png')

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\utils\vis_utils.py in plot_model(model, to_file, show_shapes, show_layer_names, rankdir)
    151       This enables in-line display of the model plots in notebooks.
    152   """
--> 153   dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
    154   _, extension = os.path.splitext(to_file)
    155   if not extension:

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\utils\vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
     70   from tensorflow.python.util import nest
     71 
---> 72   _check_pydot()
     73   dot = pydot.Dot()
     74   dot.set('rankdir', rankdir)

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\tensorflow\python\keras\utils\vis_utils.py in _check_pydot()
     47     # pydot raises a generic Exception here,
     48     # so no specific class can be caught.
---> 49     raise ImportError('Failed to import pydot. You must install pydot'
     50                       ' and graphviz for `pydotprint` to work.')
     51 

ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.

不清楚为什么我会收到此错误消息。既然我已经安装了包,还有什么要做的呢?


graphviz 已下载

【问题讨论】:

  • 我查过了。但我已经下载了 graphviz 并将其添加到我的系统路径中。查看我更新的帖子。
  • 这里有一些有用的建议。对于考虑提供帮助的读者来说,这对你来说同样重要。 (1) 你可能问的问题太多了。两天内发布八篇帖子意味着您无需等待一件事的答案,然后再跳到下一件事。这将进一步向我表明您没有进行必要的研究。 (2) 您正在添加关于感谢帮助的闲聊材料,尽管许多编辑已从您之前的帖子中删除了此内容。技术写作在这里是一种期望。可根据要求提供元参考。

标签: python tensorflow plot keras


【解决方案1】:

代码:vis_utils.py

try:
  # pydot-ng is a fork of pydot that is better maintained.
  import pydot_ng as pydot
except ImportError:
  # pydotplus is an improved version of pydot
  try:
    import pydotplus as pydot
  except ImportError:
    # Fall back on pydot if necessary.
    try:
      import pydot
    except ImportError:
      pydot = None

代码:pydot.py

def get_executable_extension():
    # type: () -> str
    if is_windows():
        return '.bat' if is_anacoda() else '.exe'
    else:
        return ''

如果在 Anaconda 环境中,请使用

conda install pydot
conda install graphviz

如果在 Python 环境中,请使用

pip install pydot

在你的路径上制作graphviz bin。

【讨论】:

    【解决方案2】:

    https://stackoverflow.com/a/58498731/12138096

    这解决了这个问题。只需通过 conda 而不是 pip 安装它们。

    conda install pydot

    conda install graphviz

    【讨论】:

      猜你喜欢
      • 2018-05-16
      • 2019-08-20
      • 2016-08-21
      • 2018-04-15
      • 2019-11-05
      • 2015-02-13
      • 2018-09-25
      • 2021-03-07
      相关资源
      最近更新 更多