6

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.

4

10 Answers 10

12

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

10

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.

  1. If you haven't installed python-dotenv, run pip install python-dotenv

  2. 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

0
8

For pycharm windows run these line on the terminal:

  • set FLASK_APP=main.py
  • $env:FLASK_APP = "main.py"
  • flask run

This works for me.

1
  • thanks i was missing the $env:FLASK_APP = "main.py" Jul 31 at 16:37
3

For individuals using powershell: Copy below into your terminal:

$env:FLASK_APP = "hello.py"

Then run:

 flask run
1

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 that app.run() is only called when executing the script directly and not when importing app from e.g. another script.

NOTE: It's hard to help you without an MRE.

1

All suggestions above didn't work for me I added the lines below and worked perfectly:

if name == "main": app.run()

1

i Just run

flask --app hello run

in terminal and it worked. where hello is the name of file without the .py extension

0

I also have the same issue... I typed in the terminal next:

$env:FLASK_APP="YOUR FULL PATH to the  file"

like this:

$env:FLASK_APP = "C:\Users\M...\D...\project...\hello_f\hello.py"
0

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.

0

Rename the main.py file into app.py and then run
$ flask db init

Not the answer you're looking for? Browse other questions tagged or ask your own question.