src/aoikhotkeydep/aoikimportutil.py
def import_module_by_name(
    mod_name,
    ns_dir=None,
    sys_use=True,
    sys_add=True,
):
    """Import a module by module name.

    @param mod_name: module name in Python namespace.

    @param ns_dir: load from which namespace dir.
    Namespace dir means the dir is considered as if it's in |sys.path|.
    If |ns_dir| is specified, only load from that dir.
    Otherwise load from any namespace dirs in |sys.path|.
    """
    #
    if ns_dir is None:
        #
        try:
            mod_obj_old = sys.modules[mod_name]
        except KeyError:
            mod_obj_old = None

        #
        if sys_use:
            #
            if mod_obj_old is not None:
                return mod_obj_old

        # 3pRKQd1
        # if not want to use existing module in "sys.modules", need re-import
        # by calling "__import__" at 2eys2rL. But "__import__" will return
        # existing module in "sys.modules", so we must delete existing module
        # before calling "__import__".
        else:
            #
            try:
                del sys.modules[mod_name]
            except KeyError:
                pass

        #
        try:
            # 2eys2rL
            __import__(mod_name)
            # raise ImportError if the module not exists.
            # raise any error from the imported module.
        except Exception:
            #
            if mod_obj_old is not None:
                # restore to "sys.modules" the old module deleted at 3pRKQd1
                sys.modules[mod_name] = mod_obj_old

            #
            raise

        #
        mod_obj = sys.modules[mod_name]

        #
        if not sys_add:
            #
            par_mod = None

            rdot_idx = mod_name.rfind('.')

            if rdot_idx != -1:
                #
                par_mod_name = mod_name[0:rdot_idx]

                mod_sname = mod_name[rdot_idx + 1:]

                # can None
                par_mod = sys.modules.get(par_mod_name, None)

            #
            if mod_obj_old is not None:
                # restore to "sys.modules" the old module deleted at 3pRKQd1
                sys.modules[mod_name] = mod_obj_old

                # restore to parent module's attribute the old module deleted
                # at 3pRKQd1
                if par_mod is not None \
                        and getattr(par_mod, mod_sname, None) is mod_obj:
                    try:
                        setattr(par_mod, mod_sname, mod_obj_old)
                    except AttributeError:
                        pass
            #
            else:
                # delete from "sys.modules" the module newly loaded at 2eys2rL.
                try:
                    del sys.modules[mod_name]
                except KeyError:
                    pass

                #
                if par_mod is not None \
                        and getattr(par_mod, mod_sname, None) is mod_obj:
                    # delete from parent module's attribute the module
                    # newly loaded at 2eys2rL.
                    try:
                        delattr(par_mod, mod_sname)
                    except AttributeError:
                        pass

        #
        return mod_obj

    #
    # assert ns_dir is not None

    #
    mod_file_name_s = mod_name.split('.')
    # |file_name| means the bare name, without extension.
    #
    # E.g. 'a.b.c' to ['a', 'b', 'c']

    #
    parent_mod_name = ''  # change in each iteration below

    mod_file_dir = ns_dir  # change in each iteration below

    for mod_file_name in mod_file_name_s:
        #
        if parent_mod_name == '':
            parent_mod_obj = None

            mod_name = mod_file_name
        else:
            parent_mod_obj = sys.modules[parent_mod_name]

            mod_name = parent_mod_name + '.' + mod_file_name

        #
        if parent_mod_obj:
            __import__(mod_name)

            mod_obj = sys.modules[mod_name]
        else:
            file_handle = None

            try:
                #
                tup = imp.find_module(mod_file_name, [mod_file_dir])
                # raise ImportError

                #
                mod_obj = imp.load_module(mod_name, *tup)
                # raise any error from the imported module.

                #
                file_handle = tup[0]
            finally:
                if file_handle is not None:
                    file_handle.close()

        #
        parent_mod_name = mod_name

        mod_file_dir = os.path.join(mod_file_dir, mod_file_name)

    #
    return mod_obj
Import a module by module name.

    @param mod_name: module name in Python namespace.

    @param ns_dir: load from which namespace dir.
    Namespace dir means the dir is considered as if it's in |sys.path|.
    If |ns_dir| is specified, only load from that dir.
    Otherwise load from any namespace dirs in |sys.path|.
    
Import a module by module name.
Import a module by module name .
def import_module_by_name(
    mod_name,
    ns_dir=None,
    sys_use=True,
    sys_add=True,
):
    if ns_dir is None:
        try:
            mod_obj_old = sys.modules[mod_name]
        except KeyError:
            mod_obj_old = None
        if sys_use:
            if mod_obj_old is not None:
                return mod_obj_old
         3pRKQd1
         if not want to use existing module in "sys.modules", need re-import
         by calling "__import__" at 2eys2rL. But "__import__" will return
         existing module in "sys.modules", so we must delete existing module
         before calling "__import__".
        else:
            try:
                del sys.modules[mod_name]
            except KeyError:
                pass
        try:
             2eys2rL
            __import__(mod_name)
             raise ImportError if the module not exists.
             raise any error from the imported module.
        except Exception:
            if mod_obj_old is not None:
                 restore to "sys.modules" the old module deleted at 3pRKQd1
                sys.modules[mod_name] = mod_obj_old
            raise
        mod_obj = sys.modules[mod_name]
        if not sys_add:
            par_mod = None
            rdot_idx = mod_name.rfind('.')
            if rdot_idx != -1:
                par_mod_name = mod_name[0:rdot_idx]
                mod_sname = mod_name[rdot_idx + 1:]
                 can None
                par_mod = sys.modules.get(par_mod_name, None)
            if mod_obj_old is not None:
                 restore to "sys.modules" the old module deleted at 3pRKQd1
                sys.modules[mod_name] = mod_obj_old
                 restore to parent module's attribute the old module deleted
                 at 3pRKQd1
                if par_mod is not None \
                        and getattr(par_mod, mod_sname, None) is mod_obj:
                    try:
                        setattr(par_mod, mod_sname, mod_obj_old)
                    except AttributeError:
                        pass
            else:
                 delete from "sys.modules" the module newly loaded at 2eys2rL.
                try:
                    del sys.modules[mod_name]
                except KeyError:
                    pass
                if par_mod is not None \
                        and getattr(par_mod, mod_sname, None) is mod_obj:
                     delete from parent module's attribute the module
                     newly loaded at 2eys2rL.
                    try:
                        delattr(par_mod, mod_sname)
                    except AttributeError:
                        pass
        return mod_obj
     assert ns_dir is not None
    mod_file_name_s = mod_name.split('.')
     |file_name| means the bare name, without extension.
     E.g. 'a.b.c' to ['a', 'b', 'c']
    parent_mod_name = ''   change in each iteration below
    mod_file_dir = ns_dir   change in each iteration below
    for mod_file_name in mod_file_name_s:
        if parent_mod_name == '':
            parent_mod_obj = None
            mod_name = mod_file_name
        else:
            parent_mod_obj = sys.modules[parent_mod_name]
            mod_name = parent_mod_name + '.' + mod_file_name
        if parent_mod_obj:
            __import__(mod_name)
            mod_obj = sys.modules[mod_name]
        else:
            file_handle = None
            try:
                tup = imp.find_module(mod_file_name, [mod_file_dir])
                 raise ImportError
                mod_obj = imp.load_module(mod_name, *tup)
                 raise any error from the imported module.
                file_handle = tup[0]
            finally:
                if file_handle is not None:
                    file_handle.close()
        parent_mod_name = mod_name
        mod_file_dir = os.path.join(mod_file_dir, mod_file_name)
    return mod_obj
def import_module_by_name ( mod_name , ns_dir = None , sys_use = True , sys_add = True , ) : if ns_dir is None : try : mod_obj_old = sys . modules [ mod_name ] except KeyError : mod_obj_old = None if sys_use : if mod_obj_old is not None : return mod_obj_old else : try : del sys . modules [ mod_name ] except KeyError : pass try : __import__ ( mod_name ) except Exception : if mod_obj_old is not None : sys . modules [ mod_name ] = mod_obj_old raise mod_obj = sys . modules [ mod_name ] if not sys_add : par_mod = None rdot_idx = mod_name . rfind ( '.' ) if rdot_idx != - 1 : par_mod_name = mod_name [ 0 : rdot_idx ] mod_sname = mod_name [ rdot_idx + 1 : ] par_mod = sys . modules . get ( par_mod_name , None ) if mod_obj_old is not None : sys . modules [ mod_name ] = mod_obj_old if par_mod is not None and getattr ( par_mod , mod_sname , None ) is mod_obj : try : setattr ( par_mod , mod_sname , mod_obj_old ) except AttributeError : pass else : try : del sys . modules [ mod_name ] except KeyError : pass if par_mod is not None and getattr ( par_mod , mod_sname , None ) is mod_obj : try : delattr ( par_mod , mod_sname ) except AttributeError : pass return mod_obj mod_file_name_s = mod_name . split ( '.' ) parent_mod_name = '' mod_file_dir = ns_dir for mod_file_name in mod_file_name_s : if parent_mod_name == '' : parent_mod_obj = None mod_name = mod_file_name else : parent_mod_obj = sys . modules [ parent_mod_name ] mod_name = parent_mod_name + '.' + mod_file_name if parent_mod_obj : __import__ ( mod_name ) mod_obj = sys . modules [ mod_name ] else : file_handle = None try : tup = imp . find_module ( mod_file_name , [ mod_file_dir ] ) mod_obj = imp . load_module ( mod_name , * tup ) file_handle = tup [ 0 ] finally : if file_handle is not None : file_handle . close ( ) parent_mod_name = mod_name mod_file_dir = os . path . join ( mod_file_dir , mod_file_name ) return mod_obj
Import a module by module name.
Import a module by module name .
"""Import a module by module name.

    @param mod_name: module name in Python namespace.

    @param ns_dir: load from which namespace dir.
    Namespace dir means the dir is considered as if it's in |sys.path|.
    If |ns_dir| is specified, only load from that dir.
    Otherwise load from any namespace dirs in |sys.path|.
    """ # # # # # 3pRKQd1 # if not want to use existing module in "sys.modules", need re-import # by calling "__import__" at 2eys2rL. But "__import__" will return # existing module in "sys.modules", so we must delete existing module # before calling "__import__". # # # 2eys2rL # raise ImportError if the module not exists. # raise any error from the imported module. # # restore to "sys.modules" the old module deleted at 3pRKQd1 # # # # # # can None # # restore to "sys.modules" the old module deleted at 3pRKQd1 # restore to parent module's attribute the old module deleted # at 3pRKQd1 # # delete from "sys.modules" the module newly loaded at 2eys2rL. # # delete from parent module's attribute the module # newly loaded at 2eys2rL. # # # assert ns_dir is not None # # |file_name| means the bare name, without extension. # # E.g. 'a.b.c' to ['a', 'b', 'c'] # # change in each iteration below # change in each iteration below # # # # raise ImportError # # raise any error from the imported module. # # #
mod_name
module name in Python namespace.
module name in Python namespace .
ns_dir
load from which namespace dir.
Namespace dir means the dir is considered as if it's in |sys.path|.
If |ns_dir| is specified, only load from that dir.
Otherwise load from any namespace dirs in |sys.path|.
load from which namespace dir . Namespace dir means the dir is considered as if it ' s in |sys . path| . If |ns_dir| is specified only load from that dir . Otherwise load from any namespace dirs in |sys . path| .
sys_use
sys_add