pallets/flask · error · NoAppException
Failed to parse arguments as literal values
Error message
Failed to parse arguments as literal values: {app_name!r}. What it means
Raised by find_app_by_string when the call expression parses but its positional or keyword arguments are not Python literals (ast.literal_eval raises ValueError). Only literal values can be supplied to a factory via FLASK_APP.
Solutions
- Pass only literal arguments: export FLASK_APP='module:create_app("production")'.
- Read environment variables inside the factory instead of passing them via FLASK_APP.
- Use a wrapper function that computes args and then calls the factory.
Example fix
// before
# export FLASK_APP='module:create_app(os.environ["CFG"])' # raises
// after
# export FLASK_APP='module:create_app("production")'
# and read os.environ inside the factory Defensive patterns
Strategy: validation
Validate before calling
import ast
expr = ast.parse(app_name.strip(), mode='eval').body
for arg in getattr(expr, 'args', []):
ast.literal_eval(arg)
for kw in getattr(expr, 'keywords', []):
ast.literal_eval(kw.value) Type guard
import ast
def args_are_literals(s) -> bool:
try:
expr = ast.parse(s.strip(), mode='eval').body
for arg in getattr(expr, 'args', []):
ast.literal_eval(arg)
return True
except (ValueError, SyntaxError):
return False Try / catch
try:
app = find_app_by_string(module, app_name)
except NoAppException:
# replace non-literal args with literal strings
raise Prevention
- Pass only literal values (strings, numbers, tuples) via FLASK_APP.
- Read environment variables inside the factory.
- Use a wrapper factory for dynamic args.
When it happens
Trigger: export FLASK_APP='module:create_app(os.environ["CONFIG"])' — arguments reference names/calls, not literals. Only values like strings, numbers, tuples are allowed.
Common situations: Trying to inject runtime/env values directly into the FLASK_APP factory call; using variable names instead of literal strings.
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 as an attribute name or function call.
- 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/6bb6ca401b913dcb.
Report an issue: GitHub.
Appendix: source
Thrown at src/flask/cli.py:159
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:
args = [ast.literal_eval(arg) for arg in expr.args]
kwargs = {
kw.arg: ast.literal_eval(kw.value)
for kw in expr.keywords
if kw.arg is not None
}
except ValueError:
# literal_eval gives cryptic error messages, show a generic
# message with the full expression instead.
raise NoAppException(
f"Failed to parse arguments as literal values: {app_name!r}."
) from None
else:
raise NoAppException(
f"Failed to parse {app_name!r} as an attribute name or function call."
)
try:
attr = getattr(module, name)
except AttributeError as e:
raise NoAppException(
f"Failed to find attribute {name!r} in {module.__name__!r}."
) from e
# If the attribute is a function, call it with any args and kwargs
# to get the real application.
if inspect.isfunction(attr):
try:View on GitHub (pinned to 3596b1ab61)