{"record":{"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/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/cli.py#L69-L105","documentation":"Raised by Flask's CLI when app-discovery exhausts all heuristics: no app/application attribute, no other Flask instance, and no create_app/make_app factory. The module simply does not expose an app the CLI can use.","triggerScenarios":"FLASK_APP points at a module that defines neither a Flask instance nor a factory; the module is a utils/config-only file.","commonSituations":"Wrong module name in FLASK_APP; module that lazily creates the app inside a function not named create_app/make_app; circular import preventing the app object from existing at import time.","solutions":["Point FLASK_APP at the module that actually defines the app: export FLASK_APP=myapp:create_app.","Rename your factory to create_app or make_app so discovery finds it.","Ensure the app object exists at module import time (avoid circular imports)."],"exampleFix":"// before\n# wsgi.py — app lives in app.py\n# export FLASK_APP=wsgi -> raises\n// after\n# export FLASK_APP=app:create_app","handlingStrategy":"validation","validationCode":"import inspect, flask\ndef module_has_app_or_factory(module):\n    has_app = any(isinstance(v, flask.Flask) for v in module.__dict__.values())\n    has_factory = any(inspect.isfunction(getattr(module, n)) for n in ('create_app','make_app'))\n    return has_app or has_factory","typeGuard":"import flask\ndef module_exports_app(module) -> bool:\n    return any(isinstance(v, flask.Flask) for v in module.__dict__.values())","tryCatchPattern":"try:\n    app = find_best_app(module)\nexcept NoAppException:\n    # instruct user to set FLASK_APP explicitly\n    raise","preventionTips":["Name factories create_app or make_app.","Ensure the app exists at import time (no circular imports).","Verify FLASK_APP points to the right module."],"tags":["cli","flask-app-env","discovery"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}