Flask says "did not provide the FLASK_APP environment variable"

rosendin picture rosendin · Nov 15, 2016 · Viewed 52.8k times · Source

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?

Answer

oxalorg picture oxalorg · Nov 17, 2016

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.