Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask says "did not provide the FLASK_APP environment variable"

Tags:

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?

like image 327
rosendin Avatar asked Nov 15 '16 22:11

rosendin


People also ask

How do I set an environment variable in flask?

Command-line: Similarly, the FLASK_ENV variable sets the environment on which we want our flask application to run. For example, if we put FLASK_ENV=development the environment will be switched to development. By default, the environment is set to development.

Could not locate a flask application you did not provide the FLASK_APP environment?

For the flask script to work, an application needs to be discovered. This is achieved by exporting the FLASK_APP environment variable. It can be either set to an import path or to a filename of a Python module that contains a Flask application.

What environment variable does flask use?

It is controlled with the FLASK_ENV environment variable and defaults to production . Setting FLASK_ENV to development will enable debug mode. flask run will use the interactive debugger and reloader by default in debug mode.

What is FLASK_APP?

FLASK_APP=hello. The name is imported, automatically detecting an app ( app ) or factory ( create_app ). FLASK_APP has three parts: an optional path that sets the current working directory, a Python file or dotted import path, and an optional variable name of the instance or factory.


1 Answers

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.

like image 127
u2gilles Avatar answered Oct 13 '22 21:10

u2gilles