pallets/flask · error · NoAppException

Detected multiple Flask applications in module

Error message

Detected multiple Flask applications in module '{module.__name__}'. Use '{module.__name__}:name' to specify the correct one.

What it means

Raised by Flask's CLI app-discovery when scanning a module finds more than one Flask instance and neither is named app/application. The CLI cannot guess which app to use, so it requires an explicit FLASK_APP target.

Solutions

  1. Set FLASK_APP=module:app_name to point at the specific instance.
  2. Rename the intended app to app or application so it is picked automatically.
  3. Remove or move unrelated Flask instances out of the target module.

Example fix

// before
# myapp.py
app1 = Flask(__name__)
app2 = Flask(__name__)
# FLASK_APP=myapp -> raises
// after
# export FLASK_APP=myapp:app1
Defensive patterns

Strategy: validation

Validate before calling

apps = [v for v in module.__dict__.values() if isinstance(v, Flask)]
if len(apps) > 1:
    raise RuntimeError('Multiple apps; set FLASK_APP=module:name')

Type guard

import flask
def is_flask_app(obj) -> bool:
    return isinstance(obj, flask.Flask)

Try / catch

try:
    app = find_best_app(module)
except NoAppException:
    # require explicit FLASK_APP
    raise

Prevention

When it happens

Trigger: Setting FLASK_APP=module where module defines two or more Flask instances (e.g. app1 = Flask(...) and app2 = Flask(...)) and neither is named app or application.

Common situations: Multiple app instances in one module during refactor; legacy app alongside a new one; test fixtures that define extra Flask objects at module scope.

Related errors


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

Appendix: source

Thrown at src/flask/cli.py:60

    """Given a module instance this tries to find the best possible
    application in the module or raises an exception.
    """
    from . import Flask

    # Search for the most common names first.
    for attr_name in ("app", "application"):
        app = getattr(module, attr_name, None)

        if isinstance(app, Flask):
            return app

    # Otherwise find the only object that is a Flask instance.
    matches = [v for v in module.__dict__.values() if isinstance(v, Flask)]

    if len(matches) == 1:
        return matches[0]
    elif len(matches) > 1:
        raise NoAppException(
            "Detected multiple Flask applications in module"
            f" '{module.__name__}'. Use '{module.__name__}:name'"
            " to specify the correct one."
        )

    # Search for app factory functions.
    for attr_name in ("create_app", "make_app"):
        app_factory = getattr(module, attr_name, None)

        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

View on GitHub (pinned to 3596b1ab61)