Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rochacbruno opened this issue Apr 28, 2018 · 5 comments · Fixed by #3711
Closed

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

rochacbruno opened this issue Apr 28, 2018 · 5 comments · Fixed by #3711
Assignees
Labels
Milestone

Comments

@rochacbruno
Copy link
Contributor

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
@rochacbruno
Copy link
Contributor Author

  • I can implement that and send a PR, just want to know if the idea is acceptable

@davidism
Copy link
Member

Anything that improves help messages is great.

@nerixim
Copy link

nerixim commented Feb 11, 2019

Looks better in flask 1.0.2, so maybe we can close this?

  • with a help option
~/flask flask --help
Traceback (most recent call last):
  File "/Users/username/.local/share/virtualenvs/flask-C1e3YqV7/lib/python3.7/site-packages/flask/cli.py", line 529, in list_commands
    rv.update(info.load_app().cli.list_commands(ctx))
  File "/Users/username/.local/share/virtualenvs/flask-C1e3YqV7/lib/python3.7/site-packages/flask/cli.py", line 384, in load_app
    'Could not locate a Flask application. You did not provide '
flask.cli.NoAppException: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
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.
  • without any options:
 ~/flask flask run   
 * 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 locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

@ThiefMaster

This comment has been minimized.

@davidism davidism added the cli label Apr 4, 2020
@pallets pallets deleted a comment from Cgspc Apr 26, 2020
@pallets pallets deleted a comment from ilyes-23 Apr 26, 2020
@davidism davidism added this to the 2.0.0 milestone Jul 30, 2020
@davidism
Copy link
Member

davidism commented Jul 30, 2020

The various exception messages, including when the env var wasn't set and a default wasn't found, have improved over time. That one now reads:

Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

Tracebacks were added in #2208 to avoid confusion caused by the previous behavior where list_commands() hid errors completely.

After reviewing the code, I think we should show only the error message for NoAppException, while still showing the full traceback for other exceptions. Tracebacks for loading errors are no longer helpful as they only show Flask internals. We still want to show the help text because the flask --help command still has useful output.

@davidism davidism self-assigned this Jul 30, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
5 participants