Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions packages/python/plotly/_plotly_utils/optional_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Stand-alone module to provide information about whether optional deps exist.

"""

from importlib import import_module
import logging
import sys
Expand All @@ -19,16 +20,18 @@ def get_module(name, should_load=True):
:return: (module|None) If import succeeds, the module will be returned.

"""
if name in sys.modules:
return sys.modules[name]
if not should_load:
return None
if name not in _not_importable:
try:
return import_module(name)
except ImportError:
_not_importable.add(name)
except Exception:
_not_importable.add(name)
msg = f"Error importing optional module {name}"
logger.exception(msg)
return sys.modules.get(name, None)

else:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else is not necessary after return.

if name not in _not_importable:
try:
return import_module(name)
except ImportError:
_not_importable.add(name)
except Exception:
_not_importable.add(name)
msg = f"Error importing optional module {name}"
logger.exception(msg)
else:
return None