From 946a3b371567af143e13c159f00051353f161c56 Mon Sep 17 00:00:00 2001 From: Ioannis Filippidis Date: Wed, 11 Apr 2018 13:55:14 -0700 Subject: [PATCH] import `pydot`, improve error messages about `pydot` and GraphViz, bump to `pydot >= 1.2.4` (#9904) * REL: bump to `pydot >= 1.2.4` in `extras_require` * MAI: import pydot (as required in `extras_require`) * MAI: refine error messages for `pydot` and GraphViz distinguish between absence of `pydot` and failure to find the executables of GraphViz in the $PATH. * DEV: ignore `.pytest_cache` --- .gitignore | 1 + keras/utils/vis_utils.py | 31 +++++++++++++++---------------- setup.py | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 028725bb660..68ec64cdb8e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ examples/img/* # test-related .coverage .cache +.pytest_cache # developer environments .idea diff --git a/keras/utils/vis_utils.py b/keras/utils/vis_utils.py index 63eedd1cbfb..0b4fa31ead5 100644 --- a/keras/utils/vis_utils.py +++ b/keras/utils/vis_utils.py @@ -5,31 +5,30 @@ import os +# `pydot` is an optional dependency, +# see `extras_require` in `setup.py`. try: - # pydot-ng is a fork of pydot that is better maintained. - import pydot_ng as pydot + import 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 = None def _check_pydot(): + """Raise errors if `pydot` or GraphViz unavailable.""" + if pydot is None: + raise ImportError( + 'Failed to import `pydot`. ' + 'Please install `pydot`. ' + 'For example with `pip install pydot`.') try: # Attempt to create an image of a blank graph # to check the pydot/graphviz installation. pydot.Dot.create(pydot.Dot()) - except Exception: - # pydot raises a generic Exception here, - # so no specific class can be caught. - raise ImportError('Failed to import pydot. You must install pydot' - ' and graphviz for `pydotprint` to work.') + except OSError: + raise OSError( + '`pydot` failed to call GraphViz.' + 'Please install GraphViz (https://www.graphviz.org/) ' + 'and ensure that its executables are in the $PATH.') def model_to_dot(model, diff --git a/setup.py b/setup.py index ae3f9a6759c..11d6d247a82 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ 'pyyaml', 'h5py'], extras_require={ - 'visualize': ['pydot>=1.2.0'], + 'visualize': ['pydot>=1.2.4'], 'tests': ['pytest', 'pytest-pep8', 'pytest-xdist',