{"id":"c7b8e9b734375f93","repo":"pallets/flask","slug":"a-valid-flask-application-was-not-obtained-from","errorCode":null,"errorMessage":"A valid Flask application was not obtained from '{module.__name__}:{app_name}'.","messagePattern":"A valid Flask application was not obtained from '(.+?):(.+?)'\\.","errorType":"exception","errorClass":"NoAppException","httpStatus":null,"severity":"error","filePath":"src/flask/cli.py","lineNumber":194,"sourceCode":"    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\n    to the search path and return the actual module name that is expected.\n    \"\"\"\n    path = os.path.realpath(path)\n\n    fname, ext = os.path.splitext(path)\n    if ext == \".py\":\n        path = fname\n\n    if os.path.basename(path) == \"__init__\":\n        path = os.path.dirname(path)\n","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/cli.py#L176-L212","documentation":"Raised at the end of `find_app_by_string` when the resolved attribute (or the factory's return value) is not an instance of `flask.Flask` (checked with `isinstance(app, Flask)`). Flask found something at the given name but it isn't a usable application object.","triggerScenarios":"`--app` pointing at a factory that returns None (forgot the `return app`), a variable holding a Blueprint, a non-function callable class instance (only `inspect.isfunction` attrs get called, so a class or functools.partial named as the target is returned as-is and fails the isinstance check), or an app wrapped in WSGI middleware (`app = ProxyFix(app)`).","commonSituations":"Application factory missing its return statement; pointing at a Blueprint or an APIRouter-style object; wrapping the app in middleware and reassigning the same variable name; two Flask installs in different virtualenvs so isinstance fails across duplicate flask modules on a broken sys.path.","solutions":["Ensure the factory ends with `return app` and that the named variable holds the Flask instance itself.","Wrap middleware on `app.wsgi_app` (`app.wsgi_app = ProxyFix(app.wsgi_app)`) instead of replacing the Flask object.","If the target is a callable class or partial rather than a plain function, wrap it in a `def` factory — Flask only auto-calls plain functions.","Check for duplicate/conflicting flask installations with `pip list` and `python -c 'import flask; print(flask.__file__)'`."],"exampleFix":"# before\ndef create_app():\n    app = Flask(__name__)\n    ...  # no return\n# after\ndef create_app():\n    app = Flask(__name__)\n    ...\n    return app","handlingStrategy":"type-guard","validationCode":"from flask import Flask\nfrom myproject import create_app\nassert isinstance(create_app(), Flask), \"factory must return a Flask instance\"","typeGuard":"from flask import Flask\ndef is_flask_app(obj):\n    return isinstance(obj, Flask)","tryCatchPattern":"try:\n    app = info.load_app()\nexcept NoAppException as e:\n    print(f\"Spec did not yield a Flask app: {e}\", file=sys.stderr)\n    sys.exit(2)","preventionTips":["Make the factory return the Flask instance — a common bug is returning None after calling app.run() or building blueprints","Point the spec at the Flask object itself, not a blueprint, wsgi wrapper, or the module","Add a return-type annotation (-> Flask) and let mypy/pyright catch wrong returns"],"tags":["flask","cli","app-discovery","app-factory"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}