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.
I am facing this error for flask run
.
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.
I am facing this error for flask run
.
I have the same issue, setting environment variable using
$env:FLASK_APP="base.py"
then
flask run
This worked for me. This works for flask db init
also
Before executing flask run
, you need to specify the flask application file by executing
export FLASK_APP=[YOUR_APP_FILE].py
(Note: make sure your application file is in the root directory)
You can create a file named .flaskenv
in your project root directory so you don't have to type in the above command every time to run your application - flask would automatically look for this file.
Quote from docs:
If python-dotenv is installed, running the flask command will set environment variables defined in the files .env and .flaskenv. This can be used to avoid having to set FLASK_APP manually every time you open a new terminal, and to set configuration using environment variables similar to how some deployment services work.
If you haven't installed python-dotenv
, run pip install python-dotenv
Create your .flaskenv file in the root directory and populate it with:
FLASK_APP=[YOUR_APP_FILE].py
After that you can safely execute flask run
For pycharm windows run these line on the terminal:
This works for me.
For individuals using powershell: Copy below into your terminal:
$env:FLASK_APP = "hello.py"
Then run:
flask run
In general this means flask can't find your flask app. This happens when, as the error message indicates, the environment doesn't contain information about FLASK_APP
(e.g. FLASK_APP="website" for website.py). You can either fix this by setting the environment variable or by using python directly.
For the environment variable could approach this with (for website.py): FLASK_APP="website" flask run
. For windows see the comment by Sync.
When using python you should rely on __name__
and app.run() for a development server. After adding the bottom part you may be able to call the script directly with python3
:
from flask import Flask
app = Flask(__name__)
if __name__ == "__main__":
app.run()
the
if
clause ensure thatapp.run()
is only called when executing the script directly and not when importingapp
from e.g. another script.
NOTE: It's hard to help you without an MRE.
All suggestions above didn't work for me I added the lines below and worked perfectly:
if name == "main": app.run()
i Just run
flask --app hello run
in terminal and it worked. where hello is the name of file without the .py extension
I had the same problem, stumbled upon this solution on Flask documentation:
$env:FLASK_APP = "hello.py" instead of "set FLASK_APP=hello.py", that is if your terminal is Windows Powershell.
https://flask.palletsprojects.com/en/1.1.x/quickstart/
Let me know if that helps.