OpenBB-finance/OpenBB · error · AttributeError

Error: The app file '{app_path}' does not contain an '{name}

Error message

Error: The app file '{app_path}' does not contain an '{name}' instance

What it means

Raised after the app module was successfully imported/loaded but it has no attribute with the requested instance name (default 'app'). The loader then cannot retrieve the FastAPI application or factory from the module.

Source

Thrown at openbb_platform/extensions/platform_api/openbb_platform_api/utils/api.py:277

                raise FileNotFoundError(  # pylint: disable=raise-missing-from
                    f"Error: Neither module '{module_path}' could be imported nor file '{file_path}' exists"
                )

            module = _load_module_from_file_path(file_path)

    # Case 2: File path (e.g., "main.py" or "my_app/main.py")
    else:
        if not Path(app_path).is_absolute():
            cwd = Path.cwd()
            app_path = str(cwd.joinpath(app_path).resolve())

        if not Path(app_path).exists():
            raise FileNotFoundError(f"Error: The app file '{app_path}' does not exist")

        module = _load_module_from_file_path(app_path)

    if not hasattr(module, name):
        raise AttributeError(
            f"Error: The app file '{app_path}' does not contain an '{name}' instance"
        )

    app_or_factory = getattr(module, name)

    # Here we use the same approach as uvicorn to handle factory functions.
    # This prevents us from relying on explicit type annotations.
    # See: https://github.com/encode/uvicorn/blob/master/uvicorn/config.py
    try:
        app = app_or_factory()
        if not factory:
            print(  # noqa: T201
                "\n\n[WARNING]   "
                "App factory detected. Using it, but please consider setting the --factory flag explicitly.\n"
            )
    except TypeError:
        if factory:
            raise TypeError(  # pylint: disable=raise-missing-from

View on GitHub (pinned to 3e071fcc2c)

Solutions

  1. Check the module's actual attribute name and pass it via the name parameter (or colon notation 'main:application').
  2. Move the FastAPI(...) assignment to module top level, outside the __main__ guard.
  3. Verify with: python -c "import main; print(hasattr(main,'app'))".
  4. Fix typos in the --name argument.

Example fix

# before
# main.py
if __name__ == "__main__":
    app = FastAPI()
import_app('main.py', 'app')  # AttributeError

# after
# main.py
app = FastAPI()
if __name__ == "__main__":
    ...
import_app('main.py', 'app')
Defensive patterns

Strategy: validation

Validate before calling

import importlib
mod = importlib.import_module("my_app.main")
assert hasattr(mod, app_name), f"module defines: {[n for n in dir(mod) if not n.startswith('_')]}"

Type guard

def module_exports(module, name: str) -> bool:
    return hasattr(module, name)

Try / catch

try:
    app = import_app("main.py", "app")
except AttributeError as e:
    # discover likely names
    import main
    raise RuntimeError(f"available names: {dir(main)}") from e

Prevention

When it happens

Trigger: Calling import_app('main.py', 'app') where main.py defines the variable under a different name (e.g. 'application', 'api', 'create_app') or only exposes it under __main__ guards.

Common situations: Uvicorn-style examples naming the app 'application'; the app assigned inside if __name__ == '__main__': so it does not exist at import time; typo in the --name parameter.

Related errors


AI-assisted analysis of OpenBB-finance/OpenBB@3e071fcc2c (2026-08-14). Data as JSON: /api/errors/9e86fab335ce978b. Report an issue: GitHub.