Skip to content

Commit 7cbd355

Browse files
committed
Add an optional_imports module.
This is a stand-alone module to the following: * Don’t import things until we need them * Keep all the information centralized * No possibility of circular imports by using this module
1 parent 62263b7 commit 7cbd355

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

plotly/optional_imports.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Stand-alone module to provide information about whether optional deps exist.
3+
4+
"""
5+
from __future__ import absolute_import
6+
7+
import importlib
8+
9+
__all__ = ['get_module']
10+
11+
import_cache = {}
12+
13+
14+
def get_module(name, raise_exc=False, msg=None):
15+
"""
16+
Return the module if it's available. Raise if desired.
17+
18+
A wrapper around importlib.import_module that only allows absolute import.
19+
20+
:param (str) name: Dot-separated module path. E.g., 'scipy.stats'.
21+
:param (bool) raise_exc: Raise ImportError if not found?
22+
:param (str|None) msg: Optional message to raise with ImportError.
23+
:raises: (ImportError) If `raise_exception` is True, this can happen.
24+
:return: (module|None) If import succeeds, the module will be returned.
25+
26+
"""
27+
if name in import_cache:
28+
return import_cache[name]
29+
30+
try:
31+
module = importlib.import_module(name) # only allow absolute imports
32+
except ImportError:
33+
if raise_exc:
34+
if msg is None:
35+
raise # just raise the current import error
36+
else:
37+
raise ImportError(msg)
38+
else:
39+
import_cache[name] = module
40+
41+
return import_cache.get(name, None) # Don't store the None!

0 commit comments

Comments
 (0)