{"record":{"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/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/cli.py#L237-L273","documentation":"In locate_app(), __import__(module_name) raised ImportError with a traceback depth of 1 (no tb_next), meaning the module itself could not be found on sys.path, and raise_if_not_found is True. Flask reports that the top-level module name is not importable.","triggerScenarios":"Setting FLASK_APP or --app to a module name that does not exist (typo), running flask from the wrong working directory so the module is not on sys.path, or pointing at a file whose prepared module name does not resolve. Distinct from error 22 because here the import failure is at the top level, not inside the module body.","commonSituations":"cd into the wrong folder and run flask; the package isn't importable. FLASK_APP points to a module that was renamed or deleted. The module lives in a package and only the bare name was given.","solutions":["Confirm the module name is spelled correctly and is importable: python -c 'import module_name'.","Run flask from the project root where the module is on sys.path, or use a dotted path (pkg.subpkg.module).","If pointing at a file, ensure prepare_import converted it to a valid module name."],"exampleFix":"# before\nexport FLASK_APP='myapp'   # no module named 'myapp' from cwd\n\n# after\ncd /path/to/project\nexport FLASK_APP='mypkg.app'","handlingStrategy":"validation","validationCode":"import importlib.util\n\ndef module_resolves(module_name: str) -> bool:\n    return importlib.util.find_spec(module_name) is not None","typeGuard":"null","tryCatchPattern":"import importlib\ntry:\n    importlib.import_module(module_name)\nexcept ModuleNotFoundError:\n    print(f'{module_name} not found on sys.path; check spelling and cwd')\n    raise","preventionTips":["Run flask from the project root or set PYTHONPATH so the module is importable.","Use dotted package paths (pkg.sub.module) rather than bare names."],"tags":["flask","cli","import","configuration"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}