{"id":"b4c8739e04504410","repo":"pallets/flask","slug":"could-not-import-module-name-r","errorCode":null,"errorMessage":"Could not import {module_name!r}.","messagePattern":"Could not import (.+?)\\.","errorType":"exception","errorClass":"NoAppException","httpStatus":null,"severity":"error","filePath":"src/flask/cli.py","lineNumber":255,"sourceCode":"    module_name: str, app_name: str | None, raise_if_not_found: t.Literal[False] = ...\n) -> Flask | None: ...\n\n\ndef locate_app(\n    module_name: str, app_name: str | None, raise_if_not_found: bool = True\n) -> Flask | None:\n    try:\n        __import__(module_name)\n    except ImportError:\n        # Reraise the ImportError if it occurred within the imported module.\n        # Determine this by checking whether the trace has a depth > 1.\n        if sys.exc_info()[2].tb_next:  # type: ignore[union-attr]\n            raise NoAppException(\n                f\"While importing {module_name!r}, an ImportError was\"\n                f\" raised:\\n\\n{traceback.format_exc()}\"\n            ) from None\n        elif raise_if_not_found:\n            raise NoAppException(f\"Could not import {module_name!r}.\") from None\n        else:\n            return None\n\n    module = sys.modules[module_name]\n\n    if app_name is None:\n        return find_best_app(module)\n    else:\n        return find_app_by_string(module, app_name)\n\n\ndef get_version(ctx: click.Context, param: click.Parameter, value: t.Any) -> None:\n    if not value or ctx.resilient_parsing:\n        return\n\n    flask_version = importlib.metadata.version(\"flask\")\n    werkzeug_version = importlib.metadata.version(\"werkzeug\")\n","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/cli.py#L237-L273","documentation":"Raised in `locate_app` when `__import__(module_name)` fails with an ImportError whose traceback has no depth — meaning the module itself could not be found at all (as opposed to error 54, where the module was found but its own imports failed). The module name is computed by `prepare_import` from the `--app` value or from wsgi.py/app.py.","triggerScenarios":"`flask --app myapp run` when there is no myapp.py / myapp package on sys.path; a path-style `--app src/myapp.py` where `prepare_import` derives a dotted name that doesn't resolve (e.g. missing `__init__.py` boundaries confusing the package walk); typo in FLASK_APP.","commonSituations":"Running `flask` from the wrong directory so the module isn't under cwd; src-layout projects where the package isn't installed (`pip install -e .` not run); misspelled module names in FLASK_APP, Dockerfiles, or Procfiles.","solutions":["Run flask from the directory containing the module, or use a file path: `flask --app path/to/myapp.py run`.","For src-layout / packaged projects, install the package into the env: `pip install -e .`.","Verify the name resolves manually: `python -c 'import myapp'` from the same directory and environment.","Check for typos in FLASK_APP / --app (module part, before any colon)."],"exampleFix":"# before (run from repo root, code in src/)\nflask --app myapp run\n# after\npip install -e .   # or: flask --app src/myapp.py run","handlingStrategy":"validation","validationCode":"import importlib.util\nif importlib.util.find_spec(\"myproject\") is None:\n    raise SystemExit(\"'myproject' not importable — run from the project root or pip install -e .\")","typeGuard":null,"tryCatchPattern":"try:\n    app = info.load_app()\nexcept NoAppException as e:\n    print(f\"{e} — check cwd, PYTHONPATH, and spelling\", file=sys.stderr)\n    sys.exit(2)","preventionTips":["Run flask from the directory containing the module, or pip install -e . so the package is on sys.path","Use dotted module paths without .py in --app: 'myproject.web', not 'myproject/web.py'","Check for name shadowing — a local file named like a stdlib/third-party package breaks resolution"],"tags":["flask","cli","import","app-discovery"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}