{"id":"141828d6ec4dda64","repo":"pallets/flask","slug":"could-not-locate-a-flask-application-use-the-fla","errorCode":null,"errorMessage":"Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.","messagePattern":"Could not locate a Flask application\\. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi\\.py' or 'app\\.py' file in the current directory\\.","errorType":"exception","errorClass":"NoAppException","httpStatus":null,"severity":"error","filePath":"src/flask/cli.py","lineNumber":359,"sourceCode":"        if self.create_app is not None:\n            app = self.create_app()\n        else:\n            if self.app_import_path:\n                path, name = (\n                    re.split(r\":(?![\\\\/])\", self.app_import_path, maxsplit=1) + [None]\n                )[:2]\n                import_name = prepare_import(path)\n                app = locate_app(import_name, name)\n            else:\n                for path in (\"wsgi.py\", \"app.py\"):\n                    import_name = prepare_import(path)\n                    app = locate_app(import_name, None, raise_if_not_found=False)\n\n                    if app is not None:\n                        break\n\n        if app is None:\n            raise NoAppException(\n                \"Could not locate a Flask application. Use the\"\n                \" 'flask --app' option, 'FLASK_APP' environment\"\n                \" variable, or a 'wsgi.py' or 'app.py' file in the\"\n                \" current directory.\"\n            )\n\n        if self.set_debug_flag:\n            # Update the app's debug flag through the descriptor so that\n            # other values repopulate as well.\n            app.debug = get_debug_flag()\n\n        self._loaded_app = app\n        return app\n\n\npass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True)\n\nF = t.TypeVar(\"F\", bound=t.Callable[..., t.Any])","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/cli.py#L341-L377","documentation":"Raised by `ScriptInfo.load_app` when every discovery path came up empty: no `create_app` callback was configured, no `--app`/FLASK_APP import path was given, and the automatic fallback probe of `wsgi.py` and `app.py` in the current directory (tried with `raise_if_not_found=False`) found no Flask app. It is Flask's generic 'I have nothing to run' error.","triggerScenarios":"Running `flask run`, `flask shell`, or `flask routes` in a directory containing neither wsgi.py nor app.py, with FLASK_APP unset and no --app option; or wsgi.py/app.py exist but contain no discoverable Flask instance or factory.","commonSituations":"Running the CLI from the repo root when the app lives in a subdirectory; the app file named something else (main.py, server.py); FLASK_APP defined in a .env file that isn't loaded because python-dotenv isn't installed; CI/Docker workdir mismatch.","solutions":["Pass the app explicitly: `flask --app path.to.module run` (or `--app module:create_app`).","Set the environment variable: `export FLASK_APP=myapp` (and install python-dotenv if you rely on .flaskenv/.env to set it).","Rename or add a wsgi.py/app.py in the working directory that exposes `app` or `create_app`, or `cd` to the directory that has one."],"exampleFix":"# before\nflask run   # app lives in server.py\n# after\nflask --app server run","handlingStrategy":"validation","validationCode":"import os, pathlib\nok = os.environ.get(\"FLASK_APP\") or any(pathlib.Path(f).exists() for f in (\"wsgi.py\", \"app.py\"))\nif not ok:\n    raise SystemExit(\"Set --app/FLASK_APP or add app.py/wsgi.py in the cwd\")","typeGuard":null,"tryCatchPattern":"try:\n    app = info.load_app()\nexcept NoAppException as e:\n    print(e, file=sys.stderr)\n    sys.exit(2)","preventionTips":["Name your entry module app.py or wsgi.py to get auto-discovery for free","Otherwise always pass --app pkg.module:attr or export FLASK_APP (a .flaskenv file makes it persistent)","Run flask from the project root — discovery only scans the current directory"],"tags":["flask","cli","app-discovery","configuration"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}