votes up 1

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.

Package:
flask
github stars 56479
Exception Class:
NoAppException

Raise code

        import_name = prepare_import(path)
                    app = locate_app(self, import_name, None, raise_if_not_found=False)

                    if app:
                        break

        if not app:
            raise 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.'
            )

        if self.set_debug_flag:
            # Update the app's debug flag through the descriptor so that
            

Ways to fix

votes up 2 votes down

This error happens when flask run command is run before setting The FLASK_APP environment variable.

How to reproduce the error:

mkdir flask_test

cd flask_test

pipenv shell

pipenv install Flask

Then write the following code inside the flask_test directory and save it as hello_flask.py

from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

Then run the following command:

flask run

The error:

 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.

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.

How to fix it:

The flask command must be told where to find your application in order to use it. The FLASK_APP environment variable is used to specify how to load the application.

To set the FLASK_APP environment run the following command.

  • On Powershell
 $env:FLASK_APP = "hello_flask"

  • CMD
set FLASK_APP=hello

  • On Bash
export FLASK_APP=hello

Then finally the command flask run can run normally.

Output:

 * Serving Flask app 'hello_flask' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Jul 29, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix