{"id":"3ebb98f849344918","repo":"pallets/flask","slug":"the-factory-app-name-r-in-module-module-name","errorCode":null,"errorMessage":"The factory {app_name!r} in module {module.__name__!r} could not be called with the specified arguments.","messagePattern":"The factory (.+?) in module (.+?) could not be called with the specified arguments\\.","errorType":"exception","errorClass":"NoAppException","httpStatus":null,"severity":"error","filePath":"src/flask/cli.py","lineNumber":183,"sourceCode":"        )\n\n    try:\n        attr = getattr(module, name)\n    except AttributeError as e:\n        raise NoAppException(\n            f\"Failed to find attribute {name!r} in {module.__name__!r}.\"\n        ) from e\n\n    # If the attribute is a function, call it with any args and kwargs\n    # to get the real application.\n    if inspect.isfunction(attr):\n        try:\n            app = attr(*args, **kwargs)\n        except TypeError as e:\n            if not _called_with_wrong_args(attr):\n                raise\n\n            raise NoAppException(\n                f\"The factory {app_name!r} in module\"\n                f\" {module.__name__!r} could not be called with the\"\n                \" specified arguments.\"\n            ) from e\n    else:\n        app = attr\n\n    if isinstance(app, Flask):\n        return app\n\n    raise NoAppException(\n        \"A valid Flask application was not obtained from\"\n        f\" '{module.__name__}:{app_name}'.\"\n    )\n\n\ndef prepare_import(path: str) -> str:\n    \"\"\"Given a filename this will try to calculate the python path, add it","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/cli.py#L165-L201","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Match the call in the spec to the factory signature, quoting literals: `flask --app 'app:create_app(\"development\")' run`.","Give factory parameters defaults (`def create_app(config_name='development')`) so the bare `--app app:create_app` form works.","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."],"exampleFix":"# before\nflask --app 'myproj:create_app' run   # def create_app(config_name): ...\n# after\nflask --app 'myproj:create_app(\"development\")' run","handlingStrategy":"validation","validationCode":"import inspect\nfrom myproject import create_app\nsig = inspect.signature(create_app)\nsig.bind(\"dev\")  # raises TypeError if ('dev') can't be passed","typeGuard":null,"tryCatchPattern":"try:\n    app = find_app_by_string(module, app_name)\nexcept NoAppException as e:\n    if e.__cause__ is not None:\n        traceback.print_exception(e.__cause__)\n    sys.exit(2)","preventionTips":["Keep factory arguments in the spec matching the factory's signature; only string literals are supported in --app calls","Give factory parameters defaults (def create_app(config=None)) so a bare create_app() always works","Remember Flask cannot pass non-string arguments via the CLI spec — do config lookup inside the factory instead"],"tags":["flask","cli","app-factory","configuration"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}