pallets/flask · error · NoAppException

Failed to find Flask application or factory in module

Error message

Failed to find Flask application or factory in module '{module.__name__}'. Use '{module.__name__}:name' to specify one.

What it means

Raised by Flask's CLI when app-discovery exhausts all heuristics: no app/application attribute, no other Flask instance, and no create_app/make_app factory. The module simply does not expose an app the CLI can use.

Solutions

  1. Point FLASK_APP at the module that actually defines the app: export FLASK_APP=myapp:create_app.
  2. Rename your factory to create_app or make_app so discovery finds it.
  3. Ensure the app object exists at module import time (avoid circular imports).

Example fix

// before
# wsgi.py — app lives in app.py
# export FLASK_APP=wsgi -> raises
// after
# export FLASK_APP=app:create_app
Defensive patterns

Strategy: validation

Validate before calling

import inspect, flask
def module_has_app_or_factory(module):
    has_app = any(isinstance(v, flask.Flask) for v in module.__dict__.values())
    has_factory = any(inspect.isfunction(getattr(module, n)) for n in ('create_app','make_app'))
    return has_app or has_factory

Type guard

import flask
def module_exports_app(module) -> bool:
    return any(isinstance(v, flask.Flask) for v in module.__dict__.values())

Try / catch

try:
    app = find_best_app(module)
except NoAppException:
    # instruct user to set FLASK_APP explicitly
    raise

Prevention

When it happens

Trigger: FLASK_APP points at a module that defines neither a Flask instance nor a factory; the module is a utils/config-only file.

Common situations: Wrong module name in FLASK_APP; module that lazily creates the app inside a function not named create_app/make_app; circular import preventing the app object from existing at import time.

Related errors


AI-assisted analysis of pallets/flask@3596b1ab61 (2026-08-11). Data as JSON: /api/errors/af958a36fa9ee795. Report an issue: GitHub.

Appendix: source

Thrown at src/flask/cli.py:87

        if inspect.isfunction(app_factory):
            try:
                app = app_factory()

                if isinstance(app, Flask):
                    return app
            except TypeError as e:
                if not _called_with_wrong_args(app_factory):
                    raise

                raise NoAppException(
                    f"Detected factory '{attr_name}' in module '{module.__name__}',"
                    " but could not call it without arguments. Use"
                    f" '{module.__name__}:{attr_name}(args)'"
                    " to specify arguments."
                ) from e

    raise NoAppException(
        "Failed to find Flask application or factory in module"
        f" '{module.__name__}'. Use '{module.__name__}:name'"
        " to specify one."
    )


def _called_with_wrong_args(f: t.Callable[..., Flask]) -> bool:
    """Check whether calling a function raised a ``TypeError`` because
    the call failed or because something in the factory raised the
    error.

    :param f: The function that was called.
    :return: ``True`` if the call failed.
    """
    tb = sys.exc_info()[2]

    try:
        while tb is not None:

View on GitHub (pinned to 3596b1ab61)