pallets/flask · error · NoAppException

Failed to find Flask application or factory in module '{modu

Error message

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

What it means

The terminal `NoAppException` from `find_best_app`: after checking for attributes named `app`/`application`, scanning for exactly one Flask instance, and probing `create_app`/`make_app` factories, Flask found nothing usable in the imported module. The module imported fine but contains no discoverable Flask application or factory under the conventional names.

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 6a2f545bfd)

Solutions

  1. Name the object explicitly: `flask --app 'module:my_app' run` or `flask --app 'module:build_app()' run`.
  2. Rename the instance to `app` or the factory to `create_app` to match Flask's discovery conventions.
  3. Move app creation out of the `if __name__ == '__main__':` block to module level (or into a factory).
  4. For packages, ensure `__init__.py` creates or imports the app, or target the correct submodule (`--app 'pkg.wsgi'`).

Example fix

# before
# server.py
my_app = Flask(__name__)
# flask --app server run  -> Failed to find Flask application or factory

# after
flask --app 'server:my_app' run
# or rename: app = Flask(__name__)
Defensive patterns

Strategy: validation

Validate before calling

# python -c "import myapp; print([n for n in dir(myapp) if n in ('app','application','create_app','make_app')])"

Prevention

When it happens

Trigger: `flask --app module run` where the module defines its Flask instance or factory under a non-standard name (e.g. `my_app = Flask(...)` or `def build_app():`), creates the app only inside `if __name__ == '__main__':`, or is a package whose `__init__.py` doesn't create/import the app.

Common situations: Pointing FLASK_APP at the package instead of the submodule that creates the app; factories named something other than create_app/make_app; app construction guarded behind main-block so `python app.py` works but `flask run` doesn't; wrong module path after a project restructure.

Related errors


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