{"record":{"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/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/cli.py#L165-L201","documentation":"Flask's CLI resolves an app from a string like 'mymodule:create_app(\"x\")'. When the named attribute is a function (an application factory), Flask calls it with the parsed positional and keyword arguments. If that call raises a TypeError AND _called_with_wrong_args confirms the signature mismatch is the cause (not an error inside the factory body), Flask raises NoAppException with this message. It signals the factory exists but its parameter list does not match the arguments you supplied in the --app/FLASK_APP import string.","triggerScenarios":"Invoking 'flask --app mypkg:create_app(\"dev\")' run, or setting FLASK_APP='mypkg:create_app(\"dev\")', where create_app's signature does not accept the literal args parsed via ast.literal_eval (e.g. create_app takes config_name but you pass an int, or you pass extra kwargs the factory does not declare). Only fires when inspect.isfunction(attr) is True and _called_with_wrong_args(attr) returns True.","commonSituations":"Migrating from the old FLASK_APP=mypkg.py form to the factory form and forgetting the (\"config\") argument, or vice versa supplying an argument to a zero-arg factory. Typing the config name with wrong quoting so literal_eval yields an unexpected type. Renaming a factory parameter so a previously-working CLI invocation no longer matches.","solutions":["Check the factory's signature (inspect.signature(create_app)) and match the --app argument list exactly, e.g. flask --app 'mypkg:create_app(\"production\")'.","Remove the call parentheses from the import string if your factory takes no arguments: flask --app mypkg:create_app (no parens) is invalid here—instead expose a module-level app or a factory with no required args.","Ensure argument literals are valid Python literals (strings must be quoted inside the already-quoted import string)."],"exampleFix":"# before\n# FLASK_APP='mypkg:create_app(\"production\")' but create_app(config_name=None)\nexport FLASK_APP='mypkg:create_app(\"production\")'\n\n# after: match the factory parameter\nexport FLASK_APP='mypkg:create_app()'   # if factory needs no args\n# or fix the factory to accept config_name\ndef create_app(config_name=\"production\"): ...","handlingStrategy":"validation","validationCode":"import inspect\n\ndef validate_factory_call(factory, args, kwargs):\n    sig = inspect.signature(factory)\n    try:\n        sig.bind(*args, **kwargs)\n    except TypeError as e:\n        raise SystemExit(f'--app args do not match factory signature: {e}')\n    return True\n\n# before setting FLASK_APP='pkg:create_app(\"x\")'\nvalidate_factory_call(create_app, ('x',), {})","typeGuard":"import inspect\n\ndef is_app_factory(obj) -> bool:\n    return inspect.isfunction(obj) and 'return' in getattr(obj, '__annotations__', {})","tryCatchPattern":"from flask.cli import NoAppException\ntry:\n    app = locate_app(module_name, app_name)\nexcept NoAppException as e:\n    print('Fix your --app / FLASK_APP string:', e)\n    raise","preventionTips":["Document the factory's expected arguments next to the CLI invocation.","Pin the factory signature with type hints and a test that binds the CLI argument literals."],"tags":["flask","cli","app-factory","configuration"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}