pallets/flask · error · NoAppException

Failed to find attribute {name!r} in {module.__name__!r}.

Error message

Failed to find attribute {name!r} in {module.__name__!r}.

What it means

Raised in `find_app_by_string` when the module imported fine but `getattr(module, name)` fails with AttributeError — the variable or factory function named after the colon in `--app module:name` does not exist in that module. Flask wraps the AttributeError in NoAppException to give a CLI-friendly message.

Source

Thrown at src/flask/cli.py:170

                kw.arg: ast.literal_eval(kw.value)
                for kw in expr.keywords
                if kw.arg is not None
            }
        except ValueError:
            # literal_eval gives cryptic error messages, show a generic
            # message with the full expression instead.
            raise NoAppException(
                f"Failed to parse arguments as literal values: {app_name!r}."
            ) from None
    else:
        raise NoAppException(
            f"Failed to parse {app_name!r} as an attribute name or function call."
        )

    try:
        attr = getattr(module, name)
    except AttributeError as e:
        raise NoAppException(
            f"Failed to find attribute {name!r} in {module.__name__!r}."
        ) from e

    # If the attribute is a function, call it with any args and kwargs
    # to get the real application.
    if inspect.isfunction(attr):
        try:
            app = attr(*args, **kwargs)
        except TypeError as e:
            if not _called_with_wrong_args(attr):
                raise

            raise NoAppException(
                f"The factory {app_name!r} in module"
                f" {module.__name__!r} could not be called with the"
                " specified arguments."
            ) from e
    else:

View on GitHub ↗ (pinned to 6a2f545bfd)

Solutions

  1. Verify the exact name exists at module top level: `python -c "import mymodule; print(dir(mymodule))"` and fix the name after the colon to match.
  2. If the factory lives in a submodule, either point at it directly (`--app 'mypkg.factory:create_app'`) or re-export it in the package's `__init__.py`.
  3. Ensure the name is defined unconditionally at import time, not inside `if __name__ == '__main__':` or another guard.

Example fix

# before
flask --app 'myproj:application' run   # myproj defines `app`
# after
flask --app 'myproj:app' run
Defensive patterns

Strategy: validation

Validate before calling

import importlib
mod = importlib.import_module("myproject")
if not hasattr(mod, "create_app"):
    raise SystemExit("myproject has no attribute 'create_app' — check the name after the colon")

Type guard

def has_app_attr(module, name):
    return hasattr(module, name)

Try / catch

try:
    app = find_app_by_string(module, app_name)
except NoAppException:
    print(sorted(n for n in dir(module) if not n.startswith('_')), file=sys.stderr)
    raise

Prevention

When it happens

Trigger: `flask --app 'mymodule:application'` when mymodule defines `app`, not `application`; `flask --app 'mypkg:create_app'` when `create_app` lives in `mypkg.factory` and isn't re-exported in `mypkg/__init__.py`; typos in the attribute name.

Common situations: Renaming the app variable or factory without updating FLASK_APP/.flaskenv/Dockerfile CMD; package `__init__.py` not importing the factory; conditional definitions (`if __name__ == '__main__'`) so the name never exists at import time; wrong module shadowing the intended one on sys.path.

Related errors


AI-assisted analysis of pallets/flask@6a2f545bfd (2026-07-31). Data as JSON: /data/errors/3ba79ffaa5757785.json. Report an issue: GitHub ↗.