pallets/flask · error · NoAppException
Could not locate a Flask application. Use the 'flask --app'…
Error message
Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
What it means
ScriptInfo.load_app() tries, in order: create_app callback, the --app/FLASK_APP import path, then the default files wsgi.py and app.py in the current directory (with raise_if_not_found=False so a missing default is silent). If after all of these app is still None, Flask raises NoAppException telling you how to specify the app.
Solutions
- Set FLASK_APP to your app location: export FLASK_APP='mypkg'.
- Use the --app CLI flag: flask --app mypkg run.
- Create an app.py or wsgi.py in the project root that imports and exposes the Flask app instance.
Example fix
# before: app lives in mypkg/__init__.py, no app.py in cwd flask run # -> Could not locate a Flask application # after export FLASK_APP='mypkg' flask run
Defensive patterns
Strategy: validation
Validate before calling
import os
def flask_app_locatable() -> bool:
if os.environ.get('FLASK_APP'):
return True
return os.path.exists('app.py') or os.path.exists('wsgi.py') Type guard
null
Try / catch
from flask.cli import NoAppException
try:
script_info.load_app()
except NoAppException:
os.environ['FLASK_APP'] = 'mypkg'
raise Prevention
- Always set FLASK_APP in your .env/flaskenv or create app.py at the project root.
- Add a startup test that asserts flask CLI can locate the app in CI.
When it happens
Trigger: Running any flask CLI command (flask run, flask routes, etc.) without --app/FLASK_APP set and without a wsgi.py or app.py file in the current directory, and no create_app registered on ScriptInfo. Each default-file locate_app returned None because raise_if_not_found=False suppressed the ImportError.
Common situations: New project where the app lives in mypkg/__init__.py but no app.py/wsgi.py exists in cwd. Renaming the entry file so the auto-detection no longer finds it. Forgetting to export FLASK_APP in a new shell session.
Related errors
- A valid Flask application was not obtained from
- Could not import .
- The environment variable
- The factory in module could not be called with the…
- Allowed methods must be a list of strings, for example…
AI-assisted analysis of pallets/flask@3596b1ab61 (2026-08-11).
Data as JSON: /api/errors/141828d6ec4dda64.
Report an issue: GitHub.
Appendix: source
Thrown at src/flask/cli.py:359
if self.create_app is not None:
app = self.create_app()
else:
if self.app_import_path:
path, name = (
re.split(r":(?![\\/])", self.app_import_path, maxsplit=1) + [None]
)[:2]
import_name = prepare_import(path)
app = locate_app(import_name, name)
else:
for path in ("wsgi.py", "app.py"):
import_name = prepare_import(path)
app = locate_app(import_name, None, raise_if_not_found=False)
if app is not None:
break
if app is None:
raise NoAppException(
"Could not locate a Flask application. Use the"
" 'flask --app' option, 'FLASK_APP' environment"
" variable, or a 'wsgi.py' or 'app.py' file in the"
" current directory."
)
if self.set_debug_flag:
# Update the app's debug flag through the descriptor so that
# other values repopulate as well.
app.debug = get_debug_flag()
self._loaded_app = app
return app
pass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True)
F = t.TypeVar("F", bound=t.Callable[..., t.Any])View on GitHub (pinned to 3596b1ab61)