{"id":"3ba79ffaa5757785","repo":"pallets/flask","slug":"failed-to-find-attribute-name-r-in-module-nam","errorCode":null,"errorMessage":"Failed to find attribute {name!r} in {module.__name__!r}.","messagePattern":"Failed to find attribute (.+?) in (.+?)\\.","errorType":"exception","errorClass":"NoAppException","httpStatus":null,"severity":"error","filePath":"src/flask/cli.py","lineNumber":170,"sourceCode":"                kw.arg: ast.literal_eval(kw.value)\n                for kw in expr.keywords\n                if kw.arg is not None\n            }\n        except ValueError:\n            # literal_eval gives cryptic error messages, show a generic\n            # message with the full expression instead.\n            raise NoAppException(\n                f\"Failed to parse arguments as literal values: {app_name!r}.\"\n            ) from None\n    else:\n        raise NoAppException(\n            f\"Failed to parse {app_name!r} as an attribute name or function call.\"\n        )\n\n    try:\n        attr = getattr(module, name)\n    except AttributeError as e:\n        raise NoAppException(\n            f\"Failed to find attribute {name!r} in {module.__name__!r}.\"\n        ) from e\n\n    # If the attribute is a function, call it with any args and kwargs\n    # to get the real application.\n    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:","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/cli.py#L152-L188","documentation":"Raised in `find_app_by_string` when the module imported fine but `getattr(module, name)` fails with AttributeError — the variable or factory function named after the colon in `--app module:name` does not exist in that module. Flask wraps the AttributeError in NoAppException to give a CLI-friendly message.","triggerScenarios":"`flask --app 'mymodule:application'` when mymodule defines `app`, not `application`; `flask --app 'mypkg:create_app'` when `create_app` lives in `mypkg.factory` and isn't re-exported in `mypkg/__init__.py`; typos in the attribute name.","commonSituations":"Renaming the app variable or factory without updating FLASK_APP/.flaskenv/Dockerfile CMD; package `__init__.py` not importing the factory; conditional definitions (`if __name__ == '__main__'`) so the name never exists at import time; wrong module shadowing the intended one on sys.path.","solutions":["Verify the exact name exists at module top level: `python -c \"import mymodule; print(dir(mymodule))\"` and fix the name after the colon to match.","If the factory lives in a submodule, either point at it directly (`--app 'mypkg.factory:create_app'`) or re-export it in the package's `__init__.py`.","Ensure the name is defined unconditionally at import time, not inside `if __name__ == '__main__':` or another guard."],"exampleFix":"# before\nflask --app 'myproj:application' run   # myproj defines `app`\n# after\nflask --app 'myproj:app' run","handlingStrategy":"validation","validationCode":"import importlib\nmod = importlib.import_module(\"myproject\")\nif not hasattr(mod, \"create_app\"):\n    raise SystemExit(\"myproject has no attribute 'create_app' — check the name after the colon\")","typeGuard":"def has_app_attr(module, name):\n    return hasattr(module, name)","tryCatchPattern":"try:\n    app = find_app_by_string(module, app_name)\nexcept NoAppException:\n    print(sorted(n for n in dir(module) if not n.startswith('_')), file=sys.stderr)\n    raise","preventionTips":["Verify the attribute exists: python -c \"import myproject; print(myproject.app)\" before running flask","Match the name after the colon exactly (case-sensitive) to the variable/factory defined at module top level","Ensure the app object is created at import time, not hidden inside if __name__ == '__main__'"],"tags":["flask","cli","app-discovery","import"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}