Return module or None. Absolute import is required. :param (str) name: Dot-separated module path. E.g., 'scipy.stats'. :raise: (ImportError) Only when exc_msg is defined. :return: (module|None) If import succeeds, the module will be returned.
(name, should_load=True)
| 12 | |
| 13 | |
| 14 | def get_module(name, should_load=True): |
| 15 | """ |
| 16 | Return module or None. Absolute import is required. |
| 17 | |
| 18 | :param (str) name: Dot-separated module path. E.g., 'scipy.stats'. |
| 19 | :raise: (ImportError) Only when exc_msg is defined. |
| 20 | :return: (module|None) If import succeeds, the module will be returned. |
| 21 | |
| 22 | """ |
| 23 | if not should_load: |
| 24 | return sys.modules.get(name, None) |
| 25 | |
| 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) |
| 35 | |
| 36 | return None |