File size: 747 Bytes
3de7bf6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
"""Import handling utilities."""
# Copyright (C) 2023-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import logging
from importlib import import_module
logger = logging.getLogger(__name__)
def try_import(import_path: str) -> bool:
"""Try to import a module.
Args:
import_path (str): The import path of the module.
Returns:
bool: True if import succeeds, False otherwise.
"""
try:
import_module(import_path)
except ImportError:
import_package = import_path.split(".")[0]
logger.warning(
f"Could not find {import_package}. To use this feature, ensure that you have {import_package} installed.",
)
else:
return True
return False
|