pallets/flask · error · NoAppException
The factory in module could not be called with the…
Error message
The factory {app_name!r} in module {module.__name__!r} could not be called with the specified arguments. What it means
Flask's CLI resolves an app from a string like 'mymodule:create_app("x")'. When the named attribute is a function (an application factory), Flask calls it with the parsed positional and keyword arguments. If that call raises a TypeError AND _called_with_wrong_args confirms the signature mismatch is the cause (not an error inside the factory body), Flask raises NoAppException with this message. It signals the factory exists but its parameter list does not match the arguments you supplied in the --app/FLASK_APP import string.
Solutions
- Check the factory's signature (inspect.signature(create_app)) and match the --app argument list exactly, e.g. flask --app 'mypkg:create_app("production")'.
- Remove the call parentheses from the import string if your factory takes no arguments: flask --app mypkg:create_app (no parens) is invalid here—instead expose a module-level app or a factory with no required args.
- Ensure argument literals are valid Python literals (strings must be quoted inside the already-quoted import string).
Example fix
# before
# FLASK_APP='mypkg:create_app("production")' but create_app(config_name=None)
export FLASK_APP='mypkg:create_app("production")'
# after: match the factory parameter
export FLASK_APP='mypkg:create_app()' # if factory needs no args
# or fix the factory to accept config_name
def create_app(config_name="production"): ... Defensive patterns
Strategy: validation
Validate before calling
import inspect
def validate_factory_call(factory, args, kwargs):
sig = inspect.signature(factory)
try:
sig.bind(*args, **kwargs)
except TypeError as e:
raise SystemExit(f'--app args do not match factory signature: {e}')
return True
# before setting FLASK_APP='pkg:create_app("x")'
validate_factory_call(create_app, ('x',), {}) Type guard
import inspect
def is_app_factory(obj) -> bool:
return inspect.isfunction(obj) and 'return' in getattr(obj, '__annotations__', {}) Try / catch
from flask.cli import NoAppException
try:
app = locate_app(module_name, app_name)
except NoAppException as e:
print('Fix your --app / FLASK_APP string:', e)
raise Prevention
- Document the factory's expected arguments next to the CLI invocation.
- Pin the factory signature with type hints and a test that binds the CLI argument literals.
When it happens
Trigger: Invoking 'flask --app mypkg:create_app("dev")' run, or setting FLASK_APP='mypkg:create_app("dev")', where create_app's signature does not accept the literal args parsed via ast.literal_eval (e.g. create_app takes config_name but you pass an int, or you pass extra kwargs the factory does not declare). Only fires when inspect.isfunction(attr) is True and _called_with_wrong_args(attr) returns True.
Common situations: Migrating from the old FLASK_APP=mypkg.py form to the factory form and forgetting the ("config") argument, or vice versa supplying an argument to a zero-arg factory. Typing the config name with wrong quoting so literal_eval yields an unexpected type. Renaming a factory parameter so a previously-working CLI invocation no longer matches.
Related errors
- A valid Flask application was not obtained from
- Could not import .
- Could not locate a Flask application. Use the 'flask --app'…
- Allowed methods must be a list of strings, for example…
- Detected multiple Flask applications in module
AI-assisted analysis of pallets/flask@3596b1ab61 (2026-08-11).
Data as JSON: /api/errors/3ebb98f849344918.
Report an issue: GitHub.
Appendix: source
Thrown at src/flask/cli.py:183
)
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:
app = attr(*args, **kwargs)
except TypeError as e:
if not _called_with_wrong_args(attr):
raise
raise NoAppException(
f"The factory {app_name!r} in module"
f" {module.__name__!r} could not be called with the"
" specified arguments."
) from e
else:
app = attr
if isinstance(app, Flask):
return app
raise NoAppException(
"A valid Flask application was not obtained from"
f" '{module.__name__}:{app_name}'."
)
def prepare_import(path: str) -> str:
"""Given a filename this will try to calculate the python path, add itView on GitHub (pinned to 3596b1ab61)