Python try import

60 Python code examples are found related to " try import". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: filesystem.py    From insightface with MIT License 8 votes vote down vote up
def try_import(package, message=None):
    """Try import specified package, with custom message support.

    Parameters
    ----------
    package : str
        The name of the targeting package.
    message : str, default is None
        If not None, this function will raise customized error message when import error is found.


    Returns
    -------
    module if found, raise ImportError otherwise

    """
    try:
        return __import__(package)
    except ImportError as e:
        if not message:
            raise e
        raise ImportError(message) 
Example 2
Source File: utils.py    From panoptic-fpn-gluon with Apache License 2.0 7 votes vote down vote up
def try_import_pycocotools():
    """Tricks to optionally install and import pycocotools"""
    # first we can try import pycocotools
    try:
        import pycocotools as _
    except ImportError:
        import os
        # we need to install pycootools, which is a bit tricky
        # pycocotools sdist requires Cython, numpy(already met)
        import_try_install('cython')
        # pypi pycocotools is not compatible with windows
        win_url = 'git+https://github.com/zhreshold/cocoapi.git#subdirectory=PythonAPI'
        try:
            if os.name == 'nt':
                import_try_install('pycocotools', win_url)
            else:
                import_try_install('pycocotools')
        except ImportError:
            faq = 'cocoapi FAQ'
            raise ImportError('Cannot import or install pycocotools, please refer to %s.' % faq) 
Example 3
Source File: vmware.py    From maas with GNU Affero General Public License v3.0 7 votes vote down vote up
def try_pyvmomi_import():
    """Attempt to import the pyVmomi API. This API is provided by the
    python3-pyvmomi package; if it doesn't work out, we need to notify
    the user so they can install it.
    """
    global vim
    global vmomi_api
    try:
        if vim is None:
            vim_module = import_module("pyVmomi")
            vim = getattr(vim_module, "vim")
        if vmomi_api is None:
            vmomi_api = import_module("pyVim.connect")
    except ImportError:
        return False
    else:
        return True 
Example 4
Source File: utils.py    From cascade_rcnn_gluon with Apache License 2.0 6 votes vote down vote up
def try_import_pycocotools():
    """Tricks to optinally install and import pycocotools"""
    # first we can try import pycocotools
    try:
        import pycocotools as _
    except ImportError:
        import os
        # we need to install pycootools, which is a bit tricky
        # pycocotools sdist requires Cython, numpy(already met)
        import_try_install('cython')
        # pypi pycocotools is not compatible with windows
        win_url = 'git+https://github.com/zhreshold/cocoapi.git#subdirectory=PythonAPI'
        try:
            if os.name == 'nt':
                import_try_install('pycocotools', win_url)
            else:
                import_try_install('pycocotools')
        except ImportError:
            faq = 'cocoapi FAQ'
            raise ImportError('Cannot import or install pycocotools, please refer to %s.' % faq) 
Example 5
Source File: moduleinfo.py    From argos with GNU General Public License v3.0 6 votes vote down vote up
def tryImportModule(self, name):
        """ Imports the module and sets version information
            If the module cannot be imported, the version is set to empty values.
        """
        self._name = name
        try:
            import importlib
            self._module = importlib.import_module(name)
        except ImportError:
            self._module = None
            self._version = ''
            self._packagePath = ''
        else:
            if self._versionAttribute:
                self._version = getattr(self._module, self._versionAttribute, '???')
            if self._pathAttribute:
                self._packagePath = getattr(self._module, self._pathAttribute, '???')


#################
# Special cases #
################# 
Example 6
Source File: registry.py    From argos with GNU General Public License v3.0 6 votes vote down vote up
def tryImportClass(self):
        """ Tries to import the registered class.
            Will set the exception property if and error occurred.
        """
        logger.info("Importing: {}".format(self.fullClassName))
        self._triedImport = True
        self._exception = None
        self._cls = None
        try:
            for pyPath in self.pythonPath.split(':'):
                if pyPath and pyPath not in sys.path:
                    logger.debug("Appending {!r} to the PythonPath.".format(pyPath))
                    sys.path.append(pyPath)
            self._cls = import_symbol(self.fullClassName) # TODO: check class?
        except Exception as ex:
            self._exception = ex
            logger.warn("Unable to import {!r}: {}".format(self.fullClassName, ex))
            if DEBUGGING:
                raise 
Example 7
Source File: filesystem.py    From awesome-semantic-segmentation-pytorch with Apache License 2.0 6 votes vote down vote up
def try_import(package, message=None):
    """Try import specified package, with custom message support.
    Parameters
    ----------
    package : str
        The name of the targeting package.
    message : str, default is None
        If not None, this function will raise customized error message when import error is found.
    Returns
    -------
    module if found, raise ImportError otherwise
    """
    try:
        return __import__(package)
    except ImportError as e:
        if not message:
            raise e
        raise ImportError(message) 
Example 8
Source File: try_import.py    From autogluon with Apache License 2.0 6 votes vote down vote up
def try_import_gluonnlp():
    try:
        import gluonnlp
        # TODO After 1.0 is supported,
        #  we will remove the checking here and use gluonnlp.utils.check_version instead.
        from pkg_resources import parse_version  # pylint: disable=import-outside-toplevel
        gluonnlp_version = parse_version(gluonnlp.__version__)
        assert gluonnlp_version >= parse_version('0.8.1') and\
               gluonnlp_version <= parse_version('0.8.3'), \
            'Currently, we only support 0.8.1<=gluonnlp<=0.8.3'
    except ImportError:
        raise ImportError(
            "Unable to import dependency gluonnlp. The NLP model won't be available "
            "without installing gluonnlp. "
            "A quick tip is to install via `pip install gluonnlp==0.8.1`. ")
    return gluonnlp 
Example 9
Source File: filesystem.py    From gluon-cv with Apache License 2.0 6 votes vote down vote up
def try_import_dali():
    """Try import NVIDIA DALI at runtime.
    """
    try:
        dali = __import__('nvidia.dali', fromlist=['pipeline', 'ops', 'types'])
        dali.Pipeline = dali.pipeline.Pipeline
    except (ImportError, RuntimeError) as e:
        if isinstance(e, ImportError):
            msg = "DALI not found, please check if you installed it correctly."
        elif isinstance(e, RuntimeError):
            msg = "No CUDA-capable device is detected ({}).".format(e)
        class dali:
            class Pipeline:
                def __init__(self):
                    raise NotImplementedError(msg)
    return dali 
Example 10
Source File: try_import.py    From autogluon with Apache License 2.0 6 votes vote down vote up
def try_import_mxnet():
    mx_version = '1.4.1'
    try:
        import mxnet as mx
        from distutils.version import LooseVersion

        if LooseVersion(mx.__version__) < LooseVersion(mx_version):
            msg = (
                "Legacy mxnet-mkl=={} detected, some new modules may not work properly. "
                "mxnet-mkl>={} is required. You can use pip to upgrade mxnet "
                "`pip install mxnet-mkl --pre --upgrade` "
                "or `pip install mxnet-cu90mkl --pre --upgrade`").format(mx.__version__, mx_version)
            raise ImportError(msg)
    except ImportError:
        raise ImportError(
            "Unable to import dependency mxnet. "
            "A quick tip is to install via `pip install mxnet-mkl/mxnet-cu90mkl --pre`. ") 
Example 11
Source File: utils.py    From habitat-api with MIT License 6 votes vote down vote up
def try_cv2_import():
    r"""The PyRobot python3 version which is a dependency of Habitat-PyRobot integration
    relies on ROS running in python2.7. In order to import cv2 in python3 we need to remove
    the python2.7 path from sys.path. To use the Habitat-PyRobot integration the user
    needs to export environment variable ROS_PATH which will look something like:
    /opt/ros/kinetic/lib/python2.7/dist-packages
    """
    import sys
    import os

    ros_path = os.environ.get("ROS_PATH")
    if ros_path is not None and ros_path in sys.path:
        sys.path.remove(ros_path)
        import cv2

        sys.path.append(ros_path)
    else:
        import cv2

    return cv2 
Example 12
Source File: import_string.py    From mishkal with GNU General Public License v3.0 6 votes vote down vote up
def try_import_module(module_name):
    """
    Imports a module, but catches import errors.  Only catches errors
    when that module doesn't exist; if that module itself has an
    import error it will still get raised.  Returns None if the module
    doesn't exist.
    """
    try:
        return import_module(module_name)
    except ImportError, e:
        if not getattr(e, 'args', None):
            raise
        desc = e.args[0]
        if not desc.startswith('No module named '):
            raise
        desc = desc[len('No module named '):]
        # If you import foo.bar.baz, the bad import could be any
        # of foo.bar.baz, bar.baz, or baz; we'll test them all:
        parts = module_name.split('.')
        for i in range(len(parts)):
            if desc == '.'.join(parts[i:]):
                return None
        raise 
Example 13
Source File: framework.py    From ray with Apache License 2.0 6 votes vote down vote up
def try_import_torch(error=False):
    """Tries importing torch and returns the module (or None).

    Args:
        error (bool): Whether to raise an error if torch cannot be imported.

    Returns:
        tuple: torch AND torch.nn modules.

    Raises:
        ImportError: If error=True and PyTorch is not installed.
    """
    if "RLLIB_TEST_NO_TORCH_IMPORT" in os.environ:
        logger.warning("Not importing PyTorch for test purposes.")
        return _torch_stubs()

    try:
        import torch
        import torch.nn as nn
        return torch, nn
    except ImportError as e:
        if error:
            raise e
        return _torch_stubs() 
Example 14
Source File: cellblender_utils.py    From cellblender with GNU General Public License v2.0 5 votes vote down vote up
def try_to_import(python_path, required_modules):
    if (required_modules == None):
        return True

    import_test = ''
    for module in required_modules:
        import_test += ('import %s' + os.linesep) % (module)
    
    cmd = [python_path, '-c', import_test] 
    process = subprocess.Popen(cmd, stderr=subprocess.PIPE)
    process.wait()
    if (process.poll()):
        return False
    else:
        return True 
Example 15
Source File: safe_import.py    From doodad with GNU General Public License v3.0 5 votes vote down vote up
def try_import(name):
    """
    A wrapper around the import statement which
    delays ImportErrors until a function is used.
    This saves writing try-catch statements around optional libraries.

    Example usage:

    # This will not throw an ImportError immediately
    badmodule = try_import('badmodule')
    badmodule.badsubmodule = try_import('badmodule.badsubmodule')

    # This will now throw an import error
    badmodule.badsubmodule.badfunction()


    Args:
        name (str): Name of module
    Returns:
        Either a module if the import was successful, or a
        FailedImportModule if the package does not exist.
    """
    try:
        return importlib.import_module(name)
    except ImportError:
        return FailedImportModule(name) 
Example 16
Source File: convert.py    From NNEF-Tools with Apache License 2.0 5 votes vote down vote up
def try_import(__import):
    if __import:
        try:
            exec(__import)
            return True
        except ImportError:
            # print("Custom function not found: {}".format(import_))
            return False
    return False 
Example 17
Source File: utils.py    From funcfinder with MIT License 5 votes vote down vote up
def try_import(module_name):
    try:
        return importlib.import_module(module_name)
    except ImportError as e:
        return _ModulePlaceholder(TryImportError(e)) 
Example 18
Source File: base.py    From demosys-py with ISC License 5 votes vote down vote up
def try_import(self, name):
        """
        Attempt to import the name.
        Raises ``ImportError`` if the name cannot be imported.

        :param name: the name to import
        """
        try:
            import_module(name)
        except ImportError:
            pass
        else:
            raise ImportError("{} conflicts with an existing python module".format(name)) 
Example 19
Source File: utils.py    From dlcookbook-dlbs with Apache License 2.0 5 votes vote down vote up
def try_import(module_name):
        """Tries to import module.
        Args:
            module_name (str): A name of a module to try to import, something like 'numpy', 'pandas', 'matplotlib' etc.
        Returns:
            bool: True if module has been imported, False otherwise.
        """
        have_module = True
        try:
            importlib.import_module(module_name)
        except ImportError:
            logging.warn("Module '%s' cannot be imported, some system information will not be available", module_name)
            have_module = False
        return have_module 
Example 20
Source File: _imports.py    From optuna with MIT License 5 votes vote down vote up
def try_import() -> _DeferredImportExceptionContextManager:
    """Create a context manager that can wrap imports of optional packages to defer exceptions.

    Returns:
        Deferred import context manager.

    """
    return _DeferredImportExceptionContextManager() 
Example 21
Source File: env.py    From cakechat with Apache License 2.0 5 votes vote down vote up
def try_import_horovod():
    try:
        import horovod.keras as hvd
    except ImportError:
        return None
    else:
        return hvd 
Example 22
Source File: common.py    From storops with Apache License 2.0 5 votes vote down vote up
def try_import(import_str, default=None):
    try:
        __import__(import_str)
        return sys.modules[import_str]
    except ImportError:
        return default 
Example 23
Source File: utils.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def try_import_module(import_str, default=None):
    """Try to import a module."""
    try:
        return import_module(import_str)
    except ImportError:
        return default 
Example 24
Source File: nova.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def try_novaapi_import(self):
        """Attempt to import the novaclient API. This API is provided by the
        python3-novaclient package; if it doesn't work out, we need to notify
        the user so they can install it.
        """
        invalidate_caches()
        try:
            if self.nova_api is None:
                self.nova_api = import_module("novaclient.client")
        except ImportError:
            return False
        else:
            return True 
Example 25
Source File: mfile.py    From xed with Apache License 2.0 5 votes vote down vote up
def try_mbuild_import():
    try:
        import mbuild
        return True
    except:
        return False 
Example 26
Source File: install_gitview.py    From pythonista-scripts with MIT License 5 votes vote down vote up
def try_import_or_install(self, progress_func=None, overwrite_existing=False):
        '''Try to import the module. Failing that, download it.'''
        try:
            self.try_import()
        except ImportError:
            self.module_install(progress_func, overwrite_existing)
            self.try_import()

    #From mark_harris' wget 
Example 27
Source File: tools.py    From biskit with GNU General Public License v3.0 5 votes vote down vote up
def tryImportModule( module, old_as=None, namespace=None ):
    """
    Try to import a module. If that fails, 'import' a dummy module
    of the same name.

    NOTE: as of python 3, the namespace returned by globals() cannot
    reach out of the tools module -- this method thus always needs to be
    called like this:

    >>> tryImportModule( 'numpy', namespace=globals() )
    
    :param module: name of the module
    :type  module: str
    :param namespace: namespace for the import [default: globals() ]
    :type  namespace: dict
    
    :return: True if import succeeded, False otherwise
    :rtype: bool
    """
    old_as = old_as or module
    g = namespace or globals()
    try:
        exec('import %s as %s' % (module, old_as), g)
        return True
    
    except ImportError as e:

        m = types.ModuleType( old_as, doc='Pseudo module. Import of real one failed.' )
        m.error = str(e)
        
        g.update( {old_as: m} )

    return False 
Example 28
Source File: tools.py    From biskit with GNU General Public License v3.0 5 votes vote down vote up
def tryImport( module, cls, old_as=None, namespace=None ):
    """
    Try to import a class from a module. If that fails, 'import' a
    default class of the same name that raises an exception when used.
    
    :param module: name of the module
    :type  module: str
    :param cls  : name of the class
    :type  cls  : str
    :param namespace: namespace for the import [default: globals() ]
    :type  namespace: dict
    
    :return: True if import succeeded, False otherwise
    :rtype: bool
    """
    old_as = old_as or cls
    g = namespace or globals()
    try:
        exec('from %s import %s as %s' % (module, cls, old_as), g)
        return True
    
    except ImportError as e:

        Cls = type( cls,(PseudoClass,),{'error':e} )
        g.update( {old_as: Cls} )

    return False 
Example 29
Source File: importutils.py    From oslo.utils with Apache License 2.0 5 votes vote down vote up
def try_import(import_str, default=None):
    """Try to import a module and if it fails return default."""
    try:
        return import_module(import_str)
    except ImportError:
        return default 
Example 30
Source File: try_import.py    From autogluon with Apache License 2.0 5 votes vote down vote up
def try_import_lightgbm():
    try:
        import lightgbm
    except OSError as e:
        raise ImportError("Import lightgbm failed. If you are using Mac OSX, "
                          "Please try 'brew install libomp'. Detailed info: {}".format(str(e))) 
Example 31
Source File: try_import.py    From autogluon with Apache License 2.0 5 votes vote down vote up
def try_import_catboost():
    try:
        import catboost
    except ValueError as e:
        raise ImportError("Import catboost failed. Numpy version may be outdated, "
                          "Please ensure numpy version >=1.16.0. If it is not, please try 'pip uninstall numpy; pip install numpy>=1.17.0' Detailed info: {}".format(str(e))) 
Example 32
Source File: try_import.py    From autogluon with Apache License 2.0 5 votes vote down vote up
def try_import_mxboard():
    try:
        import mxboard
    except ImportError:
        raise ImportError(
            "Unable to import dependency mxboard. "
            "A quick tip is to install via `pip install mxboard`. ") 
Example 33
Source File: try_import.py    From autogluon with Apache License 2.0 5 votes vote down vote up
def try_import_cv2():
    try:
        import cv2
    except ImportError:
        raise ImportError(
            "Unable to import dependency cv2. "
            "A quick tip is to install via `pip install opencv-python`. ") 
Example 34
Source File: brush.py    From MCEdit-Unified with ISC License 5 votes vote down vote up
def tryImport(self, name, dir):
        """
        Imports a brush module. Called by importBrushModules
        :param name, name of the module to import.
        """
        embeded = bool(dir == "stock-brushes")
        try:
            path = os.path.join(dir, (name + ".py"))
            if isinstance(path, unicode) and DEF_ENC != "UTF-8":
                path = path.encode(DEF_ENC)
            globals()[name] = m = imp.load_source(name, path)
            if not embeded:
                old_trn_path = albow.translate.getLangPath()
                if "trn" in sys.modules.keys():
                    del sys.modules["trn"]
                import albow.translate as trn
                trn_path = os.path.join(directories.brushesDir, name)
                if os.path.exists(trn_path):
                    trn.setLangPath(trn_path)
                    trn.buildTranslation(config.settings.langCode.get())
                m.trn = trn
                albow.translate.setLangPath(old_trn_path)
                albow.translate.buildTranslation(config.settings.langCode.get())
                self.editor.mcedit.set_update_ui(True)
                self.editor.mcedit.set_update_ui(False)
            m.materials = self.editor.level.materials
            m.tool = self
            m.createInputs(m)
            return m
        except Exception as e:
            print traceback.format_exc()
            alert(_(u"Exception while importing brush mode {}. See console for details.\n\n{}").format(name, e))
            return object() 
Example 35
Source File: __init__.py    From pyhpc-benchmarks with The Unlicense 5 votes vote down vote up
def try_import(backend):
    try:
        return importlib.import_module(f'.eos_{backend}', __name__)
    except ImportError:
        return None 
Example 36
Source File: mod.py    From HUAWEIOCR-2019 with MIT License 5 votes vote down vote up
def try_import_by_name(mod_name, error_path):
    try:
        import_by_name(mod_name)
    except ImportError:
        logging.info('adding %s to sys.path'%(error_path))
        add_to_path(error_path)        
        import_by_name(mod_name)
    
    return get_mod_by_name(mod_name) 
Example 37
Source File: filesystem.py    From awesome-semantic-segmentation-pytorch with Apache License 2.0 5 votes vote down vote up
def try_import_cv2():
    """Try import cv2 at runtime.
    Returns
    -------
    cv2 module if found. Raise ImportError otherwise
    """
    msg = "cv2 is required, you can install by package manager, e.g. 'apt-get', \
        or `pip install opencv-python --user` (note that this is unofficial PYPI package)."
    return try_import('cv2', msg) 
Example 38
Source File: filesystem.py    From insightface with MIT License 5 votes vote down vote up
def try_import_dali():
    """Try import NVIDIA DALI at runtime.
    """
    try:
        dali = __import__('nvidia.dali', fromlist=['pipeline', 'ops', 'types'])
        dali.Pipeline = dali.pipeline.Pipeline
    except ImportError:
        class dali:
            class Pipeline:
                def __init__(self):
                    raise NotImplementedError(
                        "DALI not found, please check if you installed it correctly.")
    return dali 
Example 39
Source File: filesystem.py    From insightface with MIT License 5 votes vote down vote up
def try_import_rarfile():
    """Try import rarfile at runtime.

    Returns
    -------
    rarfile module if found. Raise ImportError otherwise

    """
    msg = "rarfile is required, you can install by first `sudo apt-get install unrar` \
        and then `pip install rarfile --user` (note that this is unofficial PYPI package)."
    return try_import('rarfile', msg) 
Example 40
Source File: filesystem.py    From insightface with MIT License 5 votes vote down vote up
def try_import_cv2():
    """Try import cv2 at runtime.

    Returns
    -------
    cv2 module if found. Raise ImportError otherwise

    """
    msg = "cv2 is required, you can install by package manager, e.g. 'apt-get', \
        or `pip install opencv-python --user` (note that this is unofficial PYPI package)."
    return try_import('cv2', msg) 
Example 41
Source File: filesystem.py    From insightface with MIT License 5 votes vote down vote up
def try_import_mmcv():
    """Try import mmcv at runtime.

    Returns
    -------
    mmcv module if found. Raise ImportError otherwise

    """
    msg = "mmcv is required, you can install by first `pip install Cython --user` \
        and then `pip install mmcv --user` (note that this is unofficial PYPI package)."
    return try_import('mmcv', msg) 
Example 42
Source File: scan_perform_ext.py    From D-VAE with MIT License 5 votes vote down vote up
def try_import():
    global scan_perform
    sys.path[0:0] = [config.compiledir]
    import scan_perform
    del sys.path[0] 
Example 43
Source File: filesystem.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def try_import_decord():
    """Try import decord at runtime.

    Returns
    -------
    Decord module if found. Raise ImportError otherwise

    """
    msg = "Decord is required, you can install by `pip install decord --user` \
        (note that this is unofficial PYPI package)."
    return try_import('decord', msg) 
Example 44
Source File: filesystem.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def try_import_html5lib():
    """Try import html5lib at runtime.

    Returns
    -------
    html5lib module if found. Raise ImportError otherwise

    """
    msg = "html5lib is required, you can install by package manager, " \
          "e.g. pip install html5lib --user` (note that this is unofficial PYPI package)."
    return try_import('html5lib', msg) 
Example 45
Source File: filesystem.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def try_import_gdfDownloader():
    """Try import googleDriveFileDownloader at runtime.

    Returns
    -------
    googleDriveFileDownloader module if found. Raise ImportError otherwise

    """
    msg = "googleDriveFileDownloader is required, you can install by package manager, " \
          "e.g. pip install googleDriveFileDownloader --user` " \
          "(note that this is unofficial PYPI package)."
    return try_import('googleDriveFileDownloader', msg) 
Example 46
Source File: filesystem.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def try_import_colorama():
    """Try import colorama at runtime.

    Returns
    -------
    colorama module if found. Raise ImportError otherwise

    """
    msg = "colorama is required, you can install by `pip install colorama --user` \
         (note that this is unofficial PYPI package)."
    return try_import('colorama', msg) 
Example 47
Source File: completerlib.py    From Computable with MIT License 5 votes vote down vote up
def try_import(mod, only_modules=False):
    try:
        m = __import__(mod)
    except:
        return []
    mods = mod.split('.')
    for module in mods[1:]:
        m = getattr(m, module)

    m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__

    completions = []
    if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
        completions.extend( [attr for attr in dir(m) if
                             is_importable(m, attr, only_modules)])

    completions.extend(getattr(m, '__all__', []))
    if m_is_init:
        completions.extend(module_list(os.path.dirname(m.__file__)))
    completions = set(completions)
    if '__init__' in completions:
        completions.remove('__init__')
    return list(completions)


#-----------------------------------------------------------------------------
# Completion-related functions.
#----------------------------------------------------------------------------- 
Example 48
Source File: try_import.py    From starfish with MIT License 5 votes vote down vote up
def try_import(allowable_module_names: Optional[Set[str]]=None) -> Callable:
    """
    Decorator to apply to a method.  If one of the modules in `allowable_module_names` fail to
    import, raise a friendly error message.  If `allowable_module_names` is None, then all failed
    imports raise a friendly error message.

    Enables large and peripheral dependencies to be excluded from the build.
    """
    def _try_import_decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            try:
                func(*args, **kwargs)
            except ImportError as ex:
                module_name = ex.name

                if allowable_module_names is None or module_name in allowable_module_names:
                    raise ImportError(
                        f"{module_name} is an optional dependency of starfish. Please install "
                        f"{module_name} and its dependencies to use this functionality."
                    ) from ex
                else:
                    raise

        return wrapper

    return _try_import_decorator 
Example 49
Source File: import_helper.py    From marsnake with GNU General Public License v3.0 5 votes vote down vote up
def try_import(import_str, default=None):
    """Try to import a module and if it fails return default."""
    try:
        return import_module(import_str)
    except ImportError:
        return default 
Example 50
Source File: utils.py    From grafcli with MIT License 5 votes vote down vote up
def try_import(module_name):
    try:
        return importlib.import_module(module_name)
    except ImportError:
        return None