(obj_name: str)
Searches for the underlying module behind the name to some python object.
    Returns the module and the object name (original name with module part removed).
Searches for the underlying module behind the name to some python object.
    Returns the module and the object name (original name with module part removed).
Searches for the underlying module behind the name to some python object . Returns the module and the object name ( original name with module part removed ) .
def get_module_from_obj_name(obj_name: str) -> Tuple[types.ModuleType, str]:
    """Searches for the underlying module behind the name to some python object.
    Returns the module and the object name (original name with module part removed)."""

    # allow convenience shorthands, substitute them by full names
    obj_name = re.sub("^np.", "numpy.", obj_name)
    obj_name = re.sub("^tf.", "tensorflow.", obj_name)

    # list alternatives for (module_name, local_obj_name)
    parts = obj_name.split(".")
    name_pairs = [(".".join(parts[:i]), ".".join(parts[i:])) for i in range(len(parts), 0, -1)]

    # try each alternative in turn
    for module_name, local_obj_name in name_pairs:
        try:
            module = importlib.import_module(module_name) # may raise ImportError
            get_obj_from_module(module, local_obj_name) # may raise AttributeError
            return module, local_obj_name
        except:
            pass

    # maybe some of the modules themselves contain errors?
    for module_name, _local_obj_name in name_pairs:
        try:
            importlib.import_module(module_name) # may raise ImportError
        except ImportError:
            if not str(sys.exc_info()[1]).startswith("No module named '" + module_name + "'"):
                raise

    # maybe the requested attribute is missing?
    for module_name, local_obj_name in name_pairs:
        try:
            module = importlib.import_module(module_name) # may raise ImportError
            get_obj_from_module(module, local_obj_name) # may raise AttributeError
        except ImportError:
            pass

    # we are out of luck, but we have no idea why
    raise ImportError(obj_name)
def get_module_from_obj_name ( obj_name : str ) -> Tuple [ types . ModuleType , str ] : # allow convenience shorthands, substitute them by full names obj_name = re . sub ( "^np." , "numpy." , obj_name ) obj_name = re . sub ( "^tf." , "tensorflow." , obj_name ) # list alternatives for (module_name, local_obj_name) parts = obj_name . split ( "." ) name_pairs = [ ( "." . join ( parts [ : i ] ) , "." . join ( parts [ i : ] ) ) for i in range ( len ( parts ) , 0 , - 1 ) ] # try each alternative in turn for module_name , local_obj_name in name_pairs : try : module = importlib . import_module ( module_name ) # may raise ImportError get_obj_from_module ( module , local_obj_name ) # may raise AttributeError return module , local_obj_name except : pass # maybe some of the modules themselves contain errors? for module_name , _local_obj_name in name_pairs : try : importlib . import_module ( module_name ) # may raise ImportError except ImportError : if not str ( sys . exc_info ( ) [ 1 ] ) . startswith ( "No module named '" + module_name + "'" ) : raise # maybe the requested attribute is missing? for module_name , local_obj_name in name_pairs : try : module = importlib . import_module ( module_name ) # may raise ImportError get_obj_from_module ( module , local_obj_name ) # may raise AttributeError except ImportError : pass # we are out of luck, but we have no idea why raise ImportError ( obj_name )