pallets/flask · error · NoAppException

The factory {app_name!r} in module {module.__name__!r} could

Error message

The factory {app_name!r} in module {module.__name__!r} could not be called with the specified arguments.

What it means

Raised when the referenced attribute is a factory function and calling it with the arguments parsed from the CLI spec raises TypeError due to a signature mismatch (Flask checks via `_called_with_wrong_args` that the TypeError came from the call itself, not from inside the factory body). It means the arguments in `--app 'module:create_app(...)'` don't match the factory's parameters.

Source

Thrown at src/flask/cli.py:183

        )

    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:
        app = attr

    if isinstance(app, Flask):
        return app

    raise NoAppException(
        "A valid Flask application was not obtained from"
        f" '{module.__name__}:{app_name}'."
    )


def prepare_import(path: str) -> str:
    """Given a filename this will try to calculate the python path, add it

View on GitHub ↗ (pinned to 6a2f545bfd)

Solutions

  1. Match the call in the spec to the factory signature, quoting literals: `flask --app 'app:create_app("development")' run`.
  2. Give factory parameters defaults (`def create_app(config_name='development')`) so the bare `--app app:create_app` form works.
  3. If the TypeError actually originates inside the factory (not the call), it is re-raised directly — read the traceback to distinguish; fix the factory's internal call instead.

Example fix

# before
flask --app 'myproj:create_app' run   # def create_app(config_name): ...
# after
flask --app 'myproj:create_app("development")' run
Defensive patterns

Strategy: validation

Validate before calling

import inspect
from myproject import create_app
sig = inspect.signature(create_app)
sig.bind("dev")  # raises TypeError if ('dev') can't be passed

Try / catch

try:
    app = find_app_by_string(module, app_name)
except NoAppException as e:
    if e.__cause__ is not None:
        traceback.print_exception(e.__cause__)
    sys.exit(2)

Prevention

When it happens

Trigger: `flask --app 'app:create_app'` (called with zero args) when `create_app(config_name)` has a required positional parameter; `flask --app 'app:create_app("dev", extra=1)'` when the factory takes no `extra` kwarg; passing the wrong number of positional literals.

Common situations: Adding a required parameter to an application factory without updating run scripts; expecting Flask to pass ScriptInfo automatically (legacy behavior removed in Flask 2.x — factories are now called with only the literals you specify); copying a `--app` string between projects with different factory signatures.

Related errors


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