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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With