Skip to content

Better exception handling when env vars are missing for flask CLI #2741

@rochacbruno

Description

@rochacbruno

Expected Behavior

As there is the support for lazy loading the app, when running flask CLI without providing the proper environment variables we must see a better warning instead of raw exception.

Tell us what should happen.

we should see a better warning or message pointing to the problem

# there is no FLASK_APP env var
$ flask --help
WARNING: You need to define the app e.g: `export FLASK_APP=app.py`

Actual Behavior

Tell us what happens instead.

we see traceback before the help message

# there is no FLASK_APP env var
$ flask --help                                                               Sat 28 Apr 2018 01:25:50 PM -03
Traceback (most recent call last):
  File "~Projects/personal/flasgger/venv/lib/python3.6/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
ModuleNotFoundError: No module named 'app'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/Projects/personal/flasgger/venv/lib/python3.6/site-packages/flask/cli.py", line 529, in list_commands
    rv.update(info.load_app().cli.list_commands(ctx))
  File "~/Projects/personal/flasgger/venv/lib/python3.6/site-packages/flask/cli.py", line 372, in load_app
    app = locate_app(self, import_name, name)
  File "~/Projects/personal/flasgger/venv/lib/python3.6/site-packages/flask/cli.py", line 246, in locate_app
    'Could not import "{name}".'.format(name=module_name)
flask.cli.NoAppException: Could not import "app".
Usage: flask [OPTIONS] COMMAND [ARGS]...

  A general utility script for Flask applications.

  Provides commands from Flask, extensions, and the application. Loads the
  application defined in the FLASK_APP environment variable, or from a
  wsgi.py file. Setting the FLASK_ENV environment variable to 'development'
  will enable debug mode.

    $ export FLASK_APP=hello.py
    $ export FLASK_ENV=development
    $ flask run

Options:
  --version  Show the flask version
  --help     Show this message and exit.

Commands:
  routes  Show the routes for the app.
  run     Runs a development server.
  shell   Runs a shell in the app context.

The same happens to run

$ flask run                                                          429ms  Sat 28 Apr 2018 01:32:48 PM -03
 * Serving Flask app "app.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: Could not import "app".

The Error: Could not import "app". could include WARNING: You need to define the app e.g: export FLASK_APP=app.py

Suggestion

We could check the existence of FLASK_APP envvar before running any of the commands in the Group Cli, if FLASK_APP does not exist the dispatch of commands never happens.

Environment

  • Python version: 3.6.0
  • Flask version: Flask==1.0
  • Werkzeug version: Werkzeug==0.14.1
  • Click: click==6.7

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions