Flask Command Line Interface (CLI)
Flask includes a CLI (built on Click) that makes development workflows smooth.
How the Flask CLI finds your app
Section titled “How the Flask CLI finds your app”The CLI needs to know what Flask application to run.
Common ways:
FLASK_APP=app(points toapp.pymodule)FLASK_APP=package:create_app(factory pattern)
Example
Section titled “Example”If you have app.py with app = Flask(__name__):
export FLASK_APP=app
flask runUseful built-in commands
Section titled “Useful built-in commands”flask run— run dev serverflask routes— list all routes (very helpful!)flask shell— open a Python shell with app context (commonly used in bigger apps)
Adding custom commands (preview)
Section titled “Adding custom commands (preview)”Later, you can add custom commands like:
flask create-adminflask seed-db
Typically you’ll register them through the app factory or extensions.
Troubleshooting
Section titled “Troubleshooting”If you see errors like:
- “Could not locate a Flask application”
It usually means:
FLASK_APPis not set- your import path is wrong
- you’re in the wrong working directory
How flask finds your application
Section titled “How flask finds your application”The flask command is a click application installed alongside Flask. Before it can do
anything it has to locate your app object, and it looks in a fixed order:
flowchart TD C["flask"] --> A{"--app given?"} A -->|"yes"| U["use it"] A -->|"no"| E{"FLASK_APP set?"} E -->|"yes"| U E -->|"no"| F{"wsgi.py or app.py
in the current directory?"} F -->|"yes"| U F -->|"no"| X["Error: Could not locate a Flask application"]
Run from the wrong directory and you get exactly this, measured:
Error: 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.The fix is nearly always --app, which takes a module path rather than a file name:
flask --app app run # module app, i.e. app.py
flask --app myapp:create_app run # a factory
flask --app app routes
flask --app app shellThe three commands you get for free
Section titled “The three commands you get for free”Commands:
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context.flask routes is the fastest way to answer “why is my URL 404?” — measured on a small
app:
Endpoint Methods Rule
-------- --------- -----------------------
index GET /
items GET, POST /api/items
static GET /static/<path:filename>
user GET /user/<name>Every rule the app really has, including the automatic static one. If the route you
expected is missing, the module was never imported or the decorator never ran.
flask shell opens a REPL inside an application context, so current_app,
url_for and your database session work without any setup. A plain python REPL gives
you RuntimeError: Working outside of application context for the same code.
See it move
Section titled “See it move”Check yourself
Section titled “Check yourself”-
In what order does the flask command look for your application?
The explicit flag wins, then the environment variable, then the conventional file names. If none resolve you get 'Could not locate a Flask application'.
pch.quizShowAnswer
B — --app, then FLASK_APP, then wsgi.py or app.py in the current directory — The explicit flag wins, then the environment variable, then the conventional file names. If none resolve you get 'Could not locate a Flask application'.
-
Why does flask shell differ from running python and importing your app?
Without an application context the same code raises RuntimeError: Working outside of application context. flask shell pushes one for you.
pch.quizShowAnswer
B — it opens the REPL inside an application context, so current_app and url_for work — Without an application context the same code raises RuntimeError: Working outside of application context. flask shell pushes one for you.
-
You expect /profile to exist but get 404. What does flask routes tell you?
flask routes prints the real url_map. A missing rule is a registration problem, not a view problem — usually a blueprint that was never registered or a module never imported.
pch.quizShowAnswer
B — whether the rule is registered at all; if it is missing, the module was never imported or the decorator never ran — flask routes prints the real url_map. A missing rule is a registration problem, not a view problem — usually a blueprint that was never registered or a module never imported.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading