27

I'm trying to run a Flask application with flask run but no matter what, I receive this error:

Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.

I'm using virtualenv in my project and I'm running the app on port 80 so I run the command as superuser. Ultimately, I just need to use the flask db init command as described in Flask-Migrate's docs, but flask needs to be able to find the app to do that. Here's what I've tried, with no success:

Exporting the FLASK_APP environment variable, ensuring that it's in my bash profile, then activating virtualenv

$ export FLASK_APP=run.py
$ printenv FLASK_APP
run.py
$ . env/bin/activate
(env) $ sudo flask run
Error: Could not locate Flask application. You did not provide the     FLASK_APP environment variable.

Activating virtualenv, then exporting FLASK_APP

$ . env/bin/activate
(env) $ export FLASK_APP=run.py
(env) $ printenv FLASK_APP
run.py
(env) sudo flask run
Error: Could not locate Flask application. You did not provide the     FLASK_APP environment variable.

The above two with the full path, /Users/me/code/project/run.py

$ printenv FLASK_APP
/Users/me/code/project/run.py

Project Structure

myproject/
    ├──app/
    |  ├── __init__.py
    |  ├── models.py
    |  ├── templates/
    |  └── views.py
    ├── tests/
    ├── run.py
    ├── requirements.txt
    └── config.py

So far nothing has worked and the error message is the same in each case. What can I do to fix this error?

rosendin
  • 611
  • 1
  • 10
  • 18
  • 1
    Your file structure and exact commands you used for exporting the environment would be helpful. – oxalorg Nov 16 '16 at 09:05

7 Answers7

27

If you are on Windows, make sure there is no space around the equal :

set FLASK_APP=app.py

instead of

set FLASK_APP = app.py

That's what happened to me. I got the " You did not provide the FLASK_APP environment variable" error because of the spaces.

u2gilles
  • 6,888
  • 7
  • 51
  • 75
24

Assuming you call app=App(__name__) in your init file. Try this, even though technically it should work with run.py as-well.

export FLASK_APP=app/__init__.py; flask run

Also try doing an echo $FLASK_APP later to see if the value actually gets stored in the environment variable which flask directly accesses and not only the bash profile.

oxalorg
  • 2,768
  • 1
  • 16
  • 27
18

Under Powershell, you have to set the FLASK_APP environment variable as follows:

$env:FLASK_APP = "webapp"

Then you should be able to run python -m flask run inside the hello_app folder. In other words, PowerShell manages environment variables differently, so the standard command-line set FLASK_APP=webapp won't work.

Primusa
  • 13,136
  • 3
  • 33
  • 53
7

When I drop sudo from sudo flask run, Flask finds $FLASK_APP. However, I get the error message socket.error: [Errno 13] Permission denied. I can't see a way around this, as Flask cannot find $FLASK_APP when I run as superuser. Seems like circular logic.

I've managed to run Flask by changing the port from 80 to 5000 and dropping sudo with flask run. This is fine, I will have to find a way to run the app on port 80 in production though.

I was able to run flask db init after dropping and recreating my database, and removing calls to db.create_all.

Edit - 4/27/17 port 80 was indeed blocked by the firewall on my server (the firewall is beyond my control) so running the app on an open port resolved the issue.

rosendin
  • 611
  • 1
  • 10
  • 18
1

looks you are using bash shell in your terminal. read the flask 2.0 docs. the command should be export FLASK_APP=run and it will have no extension

Raja Khan
  • 11
  • 1
0

I faced the same issue. I am using Python 3.10 in VS code.

C:\Users\hansr>flask --version
Python 3.10.1
Flask 2.0.3
Werkzeug 2.0.3

USING set FLASK_APP='main.py' did not work for me and throws the same mentioned error above.

TRY:

$env:FLASK_APP = "main"
python -m flask run

REFERENCE

hansrajswapnil
  • 549
  • 1
  • 6
  • 14
0

I am using python3 on Windows and was getting the same error message as the original poster. Here is the code that worked for me:

set FLASK_APP=nameofyourfile.py

ann jenny
  • 581
  • 2
  • 3