pallets/flask · error · NoAppException
Failed to parse as an attribute name or function call.
Error message
Failed to parse {app_name!r} as an attribute name or function call. What it means
Raised by find_app_by_string when FLASK_APP's app-name portion cannot be parsed as a Python expression at all (ast.parse raises SyntaxError). This is the parsing fallback before attribute/call dispatch.
Solutions
- Use a valid Python identifier or call expression after the colon: FLASK_APP='module:app'.
- Quote the whole value in the shell to avoid splitting.
- Double-check balanced parentheses for call expressions.
Example fix
// before
# export FLASK_APP='module:app(' # raises
// after
# export FLASK_APP='module:app()' Defensive patterns
Strategy: validation
Validate before calling
import ast
try:
ast.parse(app_name.strip(), mode='eval')
except SyntaxError:
raise ValueError('FLASK_APP value is not a valid expression') Type guard
import ast
def is_valid_app_expr(s) -> bool:
try:
ast.parse(s.strip(), mode='eval')
return True
except SyntaxError:
return False Try / catch
try:
app = find_app_by_string(module, app_name)
except NoAppException:
# fix FLASK_APP and retry
raise Prevention
- Use a valid identifier or call expression after the colon.
- Quote the FLASK_APP value in the shell.
- Avoid stray whitespace/punctuation.
When it happens
Trigger: export FLASK_APP='module:app 123' or any value whose post-colon part is not a valid Python identifier or call expression (unbalanced parens, stray operators).
Common situations: Typos in FLASK_APP; shell quoting that mangles the value; stray whitespace/punctuation from copy-paste.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Failed to parse arguments as literal values
- Function reference must be a simple name
- Detected factory ' ' in module ' ', but could not call it…
- Detected multiple Flask applications in module
- Failed to find attribute
AI-assisted analysis of pallets/flask@3596b1ab61 (2026-08-11).
Data as JSON: /api/errors/97fb03a4ece6b66b.
Report an issue: GitHub.
Appendix: source
Thrown at src/flask/cli.py:131
return True
finally:
# Delete tb to break a circular reference.
# https://docs.python.org/2/library/sys.html#sys.exc_info
del tb
def find_app_by_string(module: ModuleType, app_name: str) -> Flask:
"""Check if the given string is a variable name or a function. Call
a function to get the app instance, or return the variable directly.
"""
from . import Flask
# Parse app_name as a single expression to determine if it's a valid
# attribute name or function call.
try:
expr = ast.parse(app_name.strip(), mode="eval").body
except SyntaxError:
raise NoAppException(
f"Failed to parse {app_name!r} as an attribute name or function call."
) from None
if isinstance(expr, ast.Name):
name = expr.id
args = []
kwargs = {}
elif isinstance(expr, ast.Call):
# Ensure the function name is an attribute name only.
if not isinstance(expr.func, ast.Name):
raise NoAppException(
f"Function reference must be a simple name: {app_name!r}."
)
name = expr.func.id
# Parse the positional and keyword arguments as literals.
try:View on GitHub (pinned to 3596b1ab61)