Home / Function/ get_root_path() — flask Function Reference

get_root_path() — flask Function Reference

Architecture documentation for the get_root_path() function in helpers.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  dc9c1c03_1812_d7dd_726c_95832bada1fd["get_root_path()"]
  881f9803_28d6_7d77_c8d7_1098b41ccf84["helpers.py"]
  dc9c1c03_1812_d7dd_726c_95832bada1fd -->|defined in| 881f9803_28d6_7d77_c8d7_1098b41ccf84
  style dc9c1c03_1812_d7dd_726c_95832bada1fd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/helpers.py lines 574–628

def get_root_path(import_name: str) -> str:
    """Find the root path of a package, or the path that contains a
    module. If it cannot be found, returns the current working
    directory.

    Not to be confused with the value returned by :func:`find_package`.

    :meta private:
    """
    # Module already imported and has a file attribute. Use that first.
    mod = sys.modules.get(import_name)

    if mod is not None and hasattr(mod, "__file__") and mod.__file__ is not None:
        return os.path.dirname(os.path.abspath(mod.__file__))

    # Next attempt: check the loader.
    try:
        spec = importlib.util.find_spec(import_name)

        if spec is None:
            raise ValueError
    except (ImportError, ValueError):
        loader = None
    else:
        loader = spec.loader

    # Loader does not exist or we're referring to an unloaded main
    # module or a main module without path (interactive sessions), go
    # with the current working directory.
    if loader is None:
        return os.getcwd()

    if hasattr(loader, "get_filename"):
        filepath = loader.get_filename(import_name)  # pyright: ignore
    else:
        # Fall back to imports.
        __import__(import_name)
        mod = sys.modules[import_name]
        filepath = getattr(mod, "__file__", None)

        # If we don't have a file path it might be because it is a
        # namespace package. In this case pick the root path from the
        # first module that is contained in the package.
        if filepath is None:
            raise RuntimeError(
                "No root path can be found for the provided module"
                f" {import_name!r}. This can happen because the module"
                " came from an import hook that does not provide file"
                " name information or because it's a namespace package."
                " In this case the root path needs to be explicitly"
                " provided."
            )

    # filepath is import_name.py for a module, or __init__.py for a package.
    return os.path.dirname(os.path.abspath(filepath))  # type: ignore[no-any-return]

Subdomains

Frequently Asked Questions

What does get_root_path() do?
get_root_path() is a function in the flask codebase, defined in src/flask/helpers.py.
Where is get_root_path() defined?
get_root_path() is defined in src/flask/helpers.py at line 574.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free