Dynamically import a module returning a handle to it.

        :param str module_path: The module path
        :rtype: module
183540
def _import_module(self, module_path):
        
        LOGGER.debug('Importing %s', module_path)
        try:
            return __import__(module_path)
        except ImportError as error:
            LOGGER.critical('Could not import %s: %s', module_path, error)
            return None
137983
def _path_hooks(cls, path):  # from importlib.PathFinder
        """"""
        if sys.path_hooks is not None and not sys.path_hooks:
            warnings.warn('sys.path_hooks is empty', ImportWarning)
        for hook in sys.path_hooks:
            try:
                return hook(path)
            except ImportError:
                continue
        else:
            return None
1181853
def import_module(module_name):
    """"""
    import sys, os
    import importlib
    sys.path.append(os.path.dirname(__file__))
    return importlib.import_module(module_name)
382740
def import_file(name, path):
    
    spec = importlib.util.spec_from_file_location(name, path)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod
905389
def importpath(path, error_text=None):
    
    result = None
    attrs = []
    parts = path.split('.')
    exception = None
    while parts:
        try:
            result = __import__('.'.join(parts), {}, {}, [''])
        except ImportError as e:
            if exception is None:
                exception = e
            attrs = parts[-1:] + attrs
            parts = parts[:-1]
        else:
            break
    for attr in attrs:
        try:
            result = getattr(result, attr)
        except (AttributeError, ValueError) as e:
            if error_text is not None:
                raise ImproperlyConfigured('Error: %s can import "%s"' % (
                    error_text, path))
            else:
                raise exception
    return result
1205558
def import_string(dotted_path: str) -> Any:
    
    try:
        module_path, class_name = dotted_path.strip(' ').rsplit('.', 1)
    except ValueError as e:
        raise ImportError(f'"{dotted_path}" doesn\'t look like a module path') from e

    module = import_module(module_path)
    try:
        return getattr(module, class_name)
    except AttributeError as e:
        raise ImportError(f'Module "{module_path}" does not define a "{class_name}" attribute') from e
1107649
def import_from(module, name):
    '''
    
    '''
    module = importlib.import_module(module)
    return getattr(module, name)
815631
def import_path(mod_path, mod_name):
    
    mod_code = open(mod_path).read()

    mod_obj = import_code(
        mod_code=mod_code,
        mod_name=mod_name,
    )

    if not hasattr(mod_obj, '__file__'):
        mod_obj.__file__ = mod_path

    return mod_obj
1268162
def get_modules(modulepath):
    """"""
    m = importlib.import_module(modulepath)
    mpath = m.__file__
    ret = set([m])

    if "__init__." in mpath.lower():
        mpath = os.path.dirname(mpath)

        # https://docs.python.org/2/library/pkgutil.html#pkgutil.iter_modules
        for module_info in pkgutil.iter_modules([mpath]):
            submodulepath = ".".join([modulepath, module_info[1]])
            if module_info[2]:
                # module is a package
                submodules = get_modules(submodulepath)
                ret.update(submodules)
            else:
                ret.add(importlib.import_module(submodulepath))

    return ret
834105
def import_string(dotted_path):
    
    try:
        module_path, class_name = dotted_path.rsplit('.', 1)
    except ValueError:
        msg = "%s doesn't look like a module path" % dotted_path
        six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])

    module = import_module(module_path)

    try:
        return getattr(module, class_name)
    except AttributeError:
        msg = 'Module "%s" does not define a "%s" attribute/class' % (
            dotted_path, class_name
        )
        six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
505150
def _import(module, cls):
    
    global Scanner

    try:
        cls = str(cls)
        mod = __import__(str(module), globals(), locals(), [cls], 1)
        Scanner = getattr(mod, cls)
    except ImportError:
        pass
753465
def from_modulename(cls, module_name):
        
        # XXX make this more robust (pyc files? zip archives? etc)
        slug = module_name.replace('.', '/')
        paths = [slug + '.py', slug + '/__init__.py']

        # always search from current directory
        for base in [''] + sys.path:
            for path in paths:
                fullpath = os.path.join(base, path)
                if os.path.exists(fullpath):
                    return cls(fullpath, prefix=module_name)
        else:
            raise ValueError("Module not found: %s" % module_name)
1230327
def import_module(path):
    
    mod = __import__(path, locals={}, globals={})
    for item in path.split('.')[1:]:
        try:
            mod = getattr(mod, item)
        except AttributeError:
            raise ImportError('No module named %s' % path)
    return mod
734162
def load(self, module_name):
        
        module_name, path = self.lookup(module_name)
        if path:
            with open(path, 'rb') as f:
                return module_name, f.read().decode('utf-8')
        return None, None
103101
def import_string(dotted_path):
    
    try:
        module_path, class_name = dotted_path.rsplit('.', 1)
    except ValueError:
        raise ImportError('%s doesn\'t look like a valid path' % dotted_path)

    module = __import__(module_path, fromlist=[class_name])

    try:
        return getattr(module, class_name)
    except AttributeError:
        msg = 'Module "%s" does not define a "%s" attribute/class' % (
            dotted_path, class_name)
        raise ImportError(msg)
224305
async def load_by_path(self, path):
        
        try:
            p, module = findModule(path, True)
        except KeyError as exc:
            raise ModuleLoadException('Cannot load module ' + repr(path) + ': ' + str(exc) + 'is not defined in the package')
        except Exception as exc:
            raise ModuleLoadException('Cannot load module ' + repr(path) + ': ' + str(exc))
        if module is None:
            raise ModuleLoadException('Cannot find module: ' + repr(path))
        return await self.loadmodule(module)
458599
def import_by_path(path: str) -> Callable:
    

    module_path, _, class_name = path.rpartition('.')
    return getattr(import_module(module_path), class_name)