Repository: bokeh/bokeh

Issue:
import_optional isn't robust to all import failures
In my IIS web app trying to import IPython raises a ValueError.

`import_requires` only protects against an ImportError so trying to import `bokeh` results in an exception which crashes the application

```
Error occurred while reading WSGI handler:

Traceback (most recent call last):
  File "c:\python\envs\py-dev\lib\site-packages\wfastcgi.py", line 779, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "c:\python\envs\py-dev\lib\site-packages\wfastcgi.py", line 621, in read_wsgi_handler
    handler = get_wsgi_handler(os.getenv('WSGI_HANDLER'))
  File "c:\python\envs\py-dev\lib\site-packages\wfastcgi.py", line 589, in get_wsgi_handler
    handler = __import__(module_name, fromlist=[name_list[0][0]])
  File ".\quantweb.py", line 42, in <module>
    app = create_app()
  File ".\quantweb.py", line 8, in create_app
    from frontend import frontend
  File ".\frontend.py", line 4, in <module>
    from utils import handle_exception
  File ".\utils.py", line 3, in <module>
    import bokeh.plotting as plt
  File "C:\dev\code\bokeh\bokeh\plotting\__init__.py", line 2, in <module>
    from ..document import Document; Document
  File "C:\dev\code\bokeh\bokeh\document.py", line 36, in <module>
    from .model import Model
  File "C:\dev\code\bokeh\bokeh\model.py", line 13, in <module>
    from .core.properties import Any, HasProps, List, MetaHasProps, String
  File "C:\dev\code\bokeh\bokeh\core\properties.py", line 78, in <module>
    IPython = import_optional('IPython')
  File "C:\dev\code\bokeh\bokeh\util\dependencies.py", line 19, in import_optional
    return import_module(mod_name)
  File "c:\python\envs\py-dev\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "c:\python\envs\py-dev\lib\site-packages\IPython\__init__.py", line 49, in <module>
    from .terminal.embed import embed
  File "c:\python\envs\py-dev\lib\site-packages\IPython\terminal\embed.py", line 16, in <module>
    from IPython.core.interactiveshell import DummyMod, InteractiveShell
  File "c:\python\envs\py-dev\lib\site-packages\IPython\core\interactiveshell.py", line 62, in <module>
    from IPython.utils import io
  File "c:\python\envs\py-dev\lib\site-packages\IPython\utils\io.py", line 92, in <module>
    stdin = IOStream(sys.stdin, fallback=devnull)
  File "c:\python\envs\py-dev\lib\site-packages\IPython\utils\io.py", line 41, in __init__
    for meth in filter(clone, dir(stream)):
  File "c:\python\envs\py-dev\lib\site-packages\IPython\utils\io.py", line 40, in clone
    return not hasattr(self, meth) and not meth.startswith('_')
  File "c:\python\envs\py-dev\lib\site-packages\IPython\utils\io.py", line 79, in closed
    return self.stream.closed
ValueError: underlying buffer has been detached


StdOut: 

StdErr: 
```



Discussion:

Resolving patch:
diff --git a/bokeh/util/dependencies.py b/bokeh/util/dependencies.py
--- a/bokeh/util/dependencies.py
+++ b/bokeh/util/dependencies.py
@@ -2,6 +2,11 @@
 
 '''
 from importlib import import_module
+import logging
+
+
+logger = logging.getLogger(__name__)
+
 
 def import_optional(mod_name):
     ''' Attempt to import an optional dependency.
@@ -18,7 +23,10 @@ def import_optional(mod_name):
     try:
         return import_module(mod_name)
     except ImportError:
-        return None
+        pass
+    except Exception:
+        msg = "Failed to import optional module `{}`".format(mod_name)
+        logger.exception(msg)
 
 def import_required(mod_name, error_msg):
     ''' Attempt to import a required dependency.


Test patch: