Skip to content

Commit 564d2d8

Browse files
committed
improved logic for optional module loading
1 parent 186bd75 commit 564d2d8

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Diff for: packages/python/plotly/_plotly_utils/optional_imports.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Stand-alone module to provide information about whether optional deps exist.
33
44
"""
5+
56
from importlib import import_module
67
import logging
78
import sys
@@ -19,16 +20,18 @@ def get_module(name, should_load=True):
1920
:return: (module|None) If import succeeds, the module will be returned.
2021
2122
"""
22-
if name in sys.modules:
23-
return sys.modules[name]
2423
if not should_load:
25-
return None
26-
if name not in _not_importable:
27-
try:
28-
return import_module(name)
29-
except ImportError:
30-
_not_importable.add(name)
31-
except Exception:
32-
_not_importable.add(name)
33-
msg = f"Error importing optional module {name}"
34-
logger.exception(msg)
24+
return sys.modules.get(name, None)
25+
26+
else:
27+
if name not in _not_importable:
28+
try:
29+
return import_module(name)
30+
except ImportError:
31+
_not_importable.add(name)
32+
except Exception:
33+
_not_importable.add(name)
34+
msg = f"Error importing optional module {name}"
35+
logger.exception(msg)
36+
else:
37+
return None

0 commit comments

Comments
 (0)