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.

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
Links to the raise (1)
https://github.com/pallets/flask/blob/83f7efa04708a22c9701488f0f29b59c2b756bef/src/flask/cli.py#L412Ways to fix
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.
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: database "sampledb" does not exist
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)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: database "sampledb" does not exist