Return imported module for the given name.
6076
def my_import(name, path=None):
    """"""

    # Documentation for importlib says this may be needed to pick up
    # modules created after the program has started
    if hasattr(importlib, 'invalidate_caches'):
        # python 3.3+
        importlib.invalidate_caches()

    if path is not None:
        directory, src_file = os.path.split(path)

        # TODO: use the importlib.util machinery
        sys.path.insert(0, directory)
        try:
            mod = importlib.import_module(name)
        finally:
            sys.path.pop(0)

    else:
        mod = importlib.import_module(name)

    return mod
879307
def get_foreign_module(namespace):
    """"""

    if namespace not in _MODULES:
        try:
            module = importlib.import_module("." + namespace, __package__)
        except ImportError:
            module = None
        _MODULES[namespace] = module

    module = _MODULES.get(namespace)
    if module is None:
        raise ForeignError("Foreign %r structs not supported" % namespace)
    return module
101901
def import_as(module, name):
    
    dir = os.path.split(__file__)[0]
    return imp.load_module(name, *imp.find_module(module, [dir]))
1296361
def import_class(self, import_name):
        
        module_name, class_name = import_name.rsplit(".", 1)
        mod = import_module(module_name)
        check_class = getattr(mod, class_name)
        self.mapping[check_class.name] = check_class
        logger.info("successfully loaded class %s", check_class)
        return check_class
638982
def load_class_by_name(name: str):
    """"""
    mod_path, _, cls_name = name.rpartition('.')
    mod = importlib.import_module(mod_path)
    cls = getattr(mod, cls_name)
    return cls
159451
def get_module(self, name, folder=None):
        """"""
        # check if this is a builtin module
        pymod = self.pycore.builtin_module(name)
        if pymod is not None:
            return pymod
        module = self.find_module(name, folder)
        if module is None:
            raise ModuleNotFoundError('Module %s not found' % name)
        return self.pycore.resource_to_pyobject(module)
1349679
def import_object(modname, name):
    
    try:
        __import__(modname)
        mod = sys.modules[modname]
        obj = mod
        for part in name.split('.'):
            obj = getattr(obj, part)
        return obj
    except:
        return None
361652
def get_from_import(self, resource, name):
        """"""
        module_name = libutils.modname(resource)
        names = []
        if isinstance(name, list):
            names = [(imported, None) for imported in name]
        else:
            names = [(name, None), ]
        return FromImport(module_name, 0, tuple(names))
826676
def _ImportModuleHookBySuffix(name, package=None):
  """"""
  _IncrementNestLevel()

  try:
    # Really import modules.
    module = _real_import_module(name, package)
  finally:
    if name.startswith('.'):
      if package:
        name = _ResolveRelativeImport(name, package)
      else:
        # Should not happen. Relative imports require the package argument.
        name = None
    if name:
      _ProcessImportBySuffix(name, None, None)

  return module
360288
def get_imports(project, pydefined):
    """"""
    pymodule = pydefined.get_module()
    module = module_imports.ModuleImports(project, pymodule)
    if pymodule == pydefined:
        return [stmt.import_info for stmt in module.imports]
    return module.get_used_imports(pydefined)
595484
def get_obj_module(qualname):
    """"""
    modname = qualname
    classname = None
    attrname = None
    while modname not in sys.modules:
        attrname = classname
        modname, classname = modname.rsplit('.', 1)

    # retrieve object and find original module name
    if classname:
        cls = getattr(sys.modules[modname], classname)
        modname = cls.__module__
        obj = getattr(cls, attrname) if attrname else cls
    else:
        obj = None

    return obj, sys.modules[modname]
587704
def get_module(mod_name):
    
    if mod_name is '':
        raise ImportError('Unable to import empty module')

    mod = __import__(mod_name)
    components = mod_name.split('.')

    for comp in components[1:]:
        mod = getattr(mod, comp)

    return mod
955770
def _get_module(module_name=None, module=None, register=True):
    
    if module is None and module_name is not None:
        try:
            module = sys.modules[module_name]
        except KeyError as ex:
            print(ex)
            raise KeyError(('module_name=%r must be loaded before ' +
                            'receiving injections') % module_name)
    elif module is not None and module_name is None:
        pass
    else:
        raise ValueError('module_name or module must be exclusively specified')
    if register is True:
        _add_injected_module(module)
    return module
1037270
def _try_import(module_name):
  """"""
  try:
    mod = importlib.import_module(module_name)
    return mod
  except ImportError:
    err_msg = ("Tried importing %s but failed. See setup.py extras_require. "
               "The dataset you are trying to use may have additional "
               "dependencies.")
    utils.reraise(err_msg)
296394
def import_object(module_name, attribute_name):
    
    # Py2 compatibility: force str (i.e bytes) when importing.
    module = __import__(str(module_name), {}, {}, [str(attribute_name)], 0)
    return getattr(module, str(attribute_name))
101980
def load_module(self, module_name, pfx=None, path=None):
        """"""
        try:
            if pfx:
                name = pfx + '.' + module_name
            else:
                name = module_name

            if name in sys.modules:
                module = sys.modules[name]
                self.logger.info("Reloading module '%s'..." % module_name)
                module = importlib.reload(module)

            else:
                self.logger.info("Loading module '%s'..." % module_name)
                module = my_import(name, path=path)

            self.module[module_name] = module
            return module

        except Exception as e:
            self.logger.error("Failed to load module '%s': %s" % (
                module_name, str(e)))
            raise ModuleManagerError(e)
638326
def import_mock(name, *args, **kwargs):
    """"""
    if any(name.startswith(s) for s in mock_modules):
        return MockModule()
    return orig_import(name, *args, **kwargs)