src/flask/scaffold.py
def _find_package_path(root_mod_name):
    """Find the path that contains the package or module."""
    try:
        spec = importlib.util.find_spec(root_mod_name)

        if spec is None:
            raise ValueError("not found")
    # ImportError: the machinery told us it does not exist
    # ValueError:
    #    - the module name was invalid
    #    - the module name is __main__
    #    - *we* raised `ValueError` due to `spec` being `None`
    except (ImportError, ValueError):
        pass  # handled below
    else:
        # namespace package
        if spec.origin in {"namespace", None}:
            return os.path.dirname(next(iter(spec.submodule_search_locations)))
        # a package (with __init__.py)
        elif spec.submodule_search_locations:
            return os.path.dirname(os.path.dirname(spec.origin))
        # just a normal module
        else:
            return os.path.dirname(spec.origin)

    # we were unable to find the `package_path` using PEP 451 loaders
    loader = pkgutil.get_loader(root_mod_name)

    if loader is None or root_mod_name == "__main__":
        # import name is not found, or interactive/main module
        return os.getcwd()

    if hasattr(loader, "get_filename"):
        filename = loader.get_filename(root_mod_name)
    elif hasattr(loader, "archive"):
        # zipimporter's loader.archive points to the .egg or .zip file.
        filename = loader.archive
    else:
        # At least one loader is missing both get_filename and archive:
        # Google App Engine's HardenedModulesHook, use __file__.
        filename = importlib.import_module(root_mod_name).__file__

    package_path = os.path.abspath(os.path.dirname(filename))

    # If the imported name is a package, filename is currently pointing
    # to the root of the package, need to get the current directory.
    if _matching_loader_thinks_module_is_package(loader, root_mod_name):
        package_path = os.path.dirname(package_path)

    return package_path
Find the path that contains the package or module.
Find the path that contains the package or module.
Find the path that contains the package or module .
def _find_package_path(root_mod_name):
    try:
        spec = importlib.util.find_spec(root_mod_name)
        if spec is None:
            raise ValueError("not found")
    except (ImportError, ValueError):
        pass  
    else:
        if spec.origin in {"namespace", None}:
            return os.path.dirname(next(iter(spec.submodule_search_locations)))
        elif spec.submodule_search_locations:
            return os.path.dirname(os.path.dirname(spec.origin))
        else:
            return os.path.dirname(spec.origin)
    loader = pkgutil.get_loader(root_mod_name)
    if loader is None or root_mod_name == "__main__":
        return os.getcwd()
    if hasattr(loader, "get_filename"):
        filename = loader.get_filename(root_mod_name)
    elif hasattr(loader, "archive"):
        filename = loader.archive
    else:
        filename = importlib.import_module(root_mod_name).__file__
    package_path = os.path.abspath(os.path.dirname(filename))
    if _matching_loader_thinks_module_is_package(loader, root_mod_name):
        package_path = os.path.dirname(package_path)
    return package_path
def _find_package_path ( root_mod_name ) : try : spec = importlib . util . find_spec ( root_mod_name ) if spec is None : raise ValueError ( "not found" ) except ( ImportError , ValueError ) : pass else : if spec . origin in { "namespace" , None } : return os . path . dirname ( next ( iter ( spec . submodule_search_locations ) ) ) elif spec . submodule_search_locations : return os . path . dirname ( os . path . dirname ( spec . origin ) ) else : return os . path . dirname ( spec . origin ) loader = pkgutil . get_loader ( root_mod_name ) if loader is None or root_mod_name == "__main__" : return os . getcwd ( ) if hasattr ( loader , "get_filename" ) : filename = loader . get_filename ( root_mod_name ) elif hasattr ( loader , "archive" ) : filename = loader . archive else : filename = importlib . import_module ( root_mod_name ) . __file__ package_path = os . path . abspath ( os . path . dirname ( filename ) ) if _matching_loader_thinks_module_is_package ( loader , root_mod_name ) : package_path = os . path . dirname ( package_path ) return package_path
Find the path that contains the package or module.
Find the path that contains the package or module .
"""Find the path that contains the package or module.""" # ImportError: the machinery told us it does not exist # ValueError: #    - the module name was invalid #    - the module name is __main__ #    - *we* raised `ValueError` due to `spec` being `None` # handled below # namespace package # a package (with __init__.py) # just a normal module # we were unable to find the `package_path` using PEP 451 loaders # import name is not found, or interactive/main module # zipimporter's loader.archive points to the .egg or .zip file. # At least one loader is missing both get_filename and archive: # Google App Engine's HardenedModulesHook, use __file__. # If the imported name is a package, filename is currently pointing # to the root of the package, need to get the current directory.
root_mod_name