(name, location=None, *, loader=None,
                            submodule_search_locations=_POPULATE)
return spec
Return a module spec based on a file location.

    To indicate that the module is a package, set
    submodule_search_locations to a list of directory paths.  An
    empty list is sufficient, though its not otherwise useful to the
    import system.

    The loader must take a spec as its only __init__() arg.
Return a module spec based on a file location.
Return a module spec based on a file location .
def spec_from_file_location(name, location=None, *, loader=None,
                            submodule_search_locations=_POPULATE):
    """Return a module spec based on a file location.

    To indicate that the module is a package, set
    submodule_search_locations to a list of directory paths.  An
    empty list is sufficient, though its not otherwise useful to the
    import system.

    The loader must take a spec as its only __init__() arg.

    """
    if location is None:
        # The caller may simply want a partially populated location-
        # oriented spec.  So we set the location to a bogus value and
        # fill in as much as we can.
        location = '<unknown>'
        if hasattr(loader, 'get_filename'):
            # ExecutionLoader
            try:
                location = loader.get_filename(name)
            except ImportError:
                pass
    else:
        location = _os.fspath(location)
        if not _path_isabs(location):
            try:
                location = _path_join(_os.getcwd(), location)
            except OSError:
                pass

    # If the location is on the filesystem, but doesn't actually exist,
    # we could return None here, indicating that the location is not
    # valid.  However, we don't have a good way of testing since an
    # indirect location (e.g. a zip file or URL) will look like a
    # non-existent file relative to the filesystem.

    spec = _bootstrap.ModuleSpec(name, loader, origin=location)
    spec._set_fileattr = True

    # Pick a loader if one wasn't provided.
    if loader is None:
        for loader_class, suffixes in _get_supported_file_loaders():
            if location.endswith(tuple(suffixes)):
                loader = loader_class(name, location)
                spec.loader = loader
                break
        else:
            return None

    # Set submodule_search_paths appropriately.
    if submodule_search_locations is _POPULATE:
        # Check the loader.
        if hasattr(loader, 'is_package'):
            try:
                is_package = loader.is_package(name)
            except ImportError:
                pass
            else:
                if is_package:
                    spec.submodule_search_locations = []
    else:
        spec.submodule_search_locations = submodule_search_locations
    if spec.submodule_search_locations == []:
        if location:
            dirname = _path_split(location)[0]
            spec.submodule_search_locations.append(dirname)

    return spec
def spec_from_file_location ( name , location = None , * , loader = None , submodule_search_locations = _POPULATE ) : if location is None : # The caller may simply want a partially populated location- # oriented spec.  So we set the location to a bogus value and # fill in as much as we can. location = '<unknown>' if hasattr ( loader , 'get_filename' ) : # ExecutionLoader try : location = loader . get_filename ( name ) except ImportError : pass else : location = _os . fspath ( location ) if not _path_isabs ( location ) : try : location = _path_join ( _os . getcwd ( ) , location ) except OSError : pass # If the location is on the filesystem, but doesn't actually exist, # we could return None here, indicating that the location is not # valid.  However, we don't have a good way of testing since an # indirect location (e.g. a zip file or URL) will look like a # non-existent file relative to the filesystem. spec = _bootstrap . ModuleSpec ( name , loader , origin = location ) spec . _set_fileattr = True # Pick a loader if one wasn't provided. if loader is None : for loader_class , suffixes in _get_supported_file_loaders ( ) : if location . endswith ( tuple ( suffixes ) ) : loader = loader_class ( name , location ) spec . loader = loader break else : return None # Set submodule_search_paths appropriately. if submodule_search_locations is _POPULATE : # Check the loader. if hasattr ( loader , 'is_package' ) : try : is_package = loader . is_package ( name ) except ImportError : pass else : if is_package : spec . submodule_search_locations = [ ] else : spec . submodule_search_locations = submodule_search_locations if spec . submodule_search_locations == [ ] : if location : dirname = _path_split ( location ) [ 0 ] spec . submodule_search_locations . append ( dirname ) return spec