“ ImportError:导入pydot失败。必须安装pydot和graphviz才能使pydotprint正常工作。”但是两者都已安装

时间:2019-09-26 11:03:20

标签: python tensorflow plot keras

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

enter image description here

但是,当我尝试绘制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已下载

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

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

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

conda install pydot

conda install graphviz

答案 1 :(得分:0)

代码: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。