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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Sai Kireeti
  • 87
  • 1
  • 1
  • 2
  • Plaese refer these https://stackoverflow.com/questions/51119495/how-to-setup-environment-variables-for-flask-run-on-windows https://github.com/Microsoft/vscode-docs/issues/1881 – 11738472 Mar 14 '21 at 17:27
  • 1
    Does this answer your question? [Flask says "did not provide the FLASK\_APP environment variable"](https://stackoverflow.com/questions/40620674/flask-says-did-not-provide-the-flask-app-environment-variable) – Nikolay Shebanov Mar 15 '21 at 14:19
  • https://stackoverflow.com/questions/37826016/env-variables-not-set-while-running-minimal-flask-application – Deyan Romanov Sep 29 '22 at 13:36
  • This will fix it [enter link description here](https://stackoverflow.com/questions/37826016/env-variables-not-set-while-running-minimal-flask-application) – Deyan Romanov Sep 29 '22 at 13:38

10 Answers10

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

rhaegnar
  • 151
  • 5
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

Parzival
  • 2,051
  • 4
  • 14
  • 32
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.

pritom_roy
  • 81
  • 1
  • 2
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.

Cobalt
  • 447
  • 5
  • 9
1

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

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

Durgam
  • 11
  • 1
1

i Just run

flask --app hello run

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

Oche Onoda
  • 11
  • 1
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"
Kuro Neko
  • 795
  • 12
  • 19
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

Arjun M S
  • 31
  • 5