{"id":"af958a36fa9ee795","repo":"pallets/flask","slug":"failed-to-find-flask-application-or-factory-in-mod","errorCode":null,"errorMessage":"Failed to find Flask application or factory in module '{module.__name__}'. Use '{module.__name__}:name' to specify one.","messagePattern":"Failed to find Flask application or factory in module '(.+?)'\\. Use '(.+?):name' to specify one\\.","errorType":"exception","errorClass":"NoAppException","httpStatus":null,"severity":"error","filePath":"src/flask/cli.py","lineNumber":87,"sourceCode":"\n        if inspect.isfunction(app_factory):\n            try:\n                app = app_factory()\n\n                if isinstance(app, Flask):\n                    return app\n            except TypeError as e:\n                if not _called_with_wrong_args(app_factory):\n                    raise\n\n                raise NoAppException(\n                    f\"Detected factory '{attr_name}' in module '{module.__name__}',\"\n                    \" but could not call it without arguments. Use\"\n                    f\" '{module.__name__}:{attr_name}(args)'\"\n                    \" to specify arguments.\"\n                ) from e\n\n    raise NoAppException(\n        \"Failed to find Flask application or factory in module\"\n        f\" '{module.__name__}'. Use '{module.__name__}:name'\"\n        \" to specify one.\"\n    )\n\n\ndef _called_with_wrong_args(f: t.Callable[..., Flask]) -> bool:\n    \"\"\"Check whether calling a function raised a ``TypeError`` because\n    the call failed or because something in the factory raised the\n    error.\n\n    :param f: The function that was called.\n    :return: ``True`` if the call failed.\n    \"\"\"\n    tb = sys.exc_info()[2]\n\n    try:\n        while tb is not None:","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/cli.py#L69-L105","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Name the object explicitly: `flask --app 'module:my_app' run` or `flask --app 'module:build_app()' run`.","Rename the instance to `app` or the factory to `create_app` to match Flask's discovery conventions.","Move app creation out of the `if __name__ == '__main__':` block to module level (or into a factory).","For packages, ensure `__init__.py` creates or imports the app, or target the correct submodule (`--app 'pkg.wsgi'`)."],"exampleFix":"# before\n# server.py\nmy_app = Flask(__name__)\n# flask --app server run  -> Failed to find Flask application or factory\n\n# after\nflask --app 'server:my_app' run\n# or rename: app = Flask(__name__)","handlingStrategy":"validation","validationCode":"# python -c \"import myapp; print([n for n in dir(myapp) if n in ('app','application','create_app','make_app')])\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Name the instance 'app' or 'application', or the factory 'create_app'/'make_app', so auto-detection finds it","Otherwise always pass the explicit reference: flask --app mymodule:my_custom_name run","Don't hide the app instance inside functions or classes if you rely on auto-discovery"],"tags":["flask","cli","app-discovery","configuration"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}