Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask app getting error of "could not locate flask application. .....FLASK_APP environment variable" for Flask Migrate

I have a file db_table.py that looks like:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost/mydb'

db = SQLAlchemy(app)
migrate = Migrate(app, db)
.....db tables....

When I try to run:

flask db init

I get:

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

I tried first manually setting FLASK_APP var, by doing set FLASK_APP=app.py then running flask db init again, but that didn't resolve the issue.

like image 367
Jshee Avatar asked Jan 03 '17 16:01

Jshee


People also ask

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.

How do you add an environment variable to a flask?

If you're on Windows 10, search "View Advanced System Settings." Then click environmental variables, hit new user variable and make it FLASK_APP and set the path where it asks. Then do flask run in terminal.

How do I export flask app from Windows?

Instead of using FLASK_APP and letting Flask load your application, you can create your own Click object and export it as a console script entry point. Create an instance of FlaskGroup and pass it the factory: import click from flask import Flask from flask.


1 Answers

The flask command line argument needs to know what module to locate the current Flask app instance in.

Set FLASK_APP as an environment variable:

export FLASK_APP=db_table.py

before running your flask command-line app.

See the Command Line Interface documentation:

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.

You can also set the variable per command by setting it on the same command line:

FLASK_APP=db_table.py flask db init
like image 113
Martijn Pieters Avatar answered Sep 25 '22 00:09

Martijn Pieters