Error launching Flask app with error "Failed to find Flask application"
Reproducing the problem, and fixing it
Hello, I have reproduced the problem This is the error code:
Error: Failed to find Flask application or factory in module "src.app". Use "FLASK_APP=src.app:name to specify one.
Steps of reproducing the error:
- Create a file called app.py
- inside app.py put this code:
from flask import Flask
def create_app():
app = Flask("abc")
@app.route('/')
def hello_world():
return 'Hello, World!'
Or let the file be empty, like this:
# This is an empty file
# There is no flask application here
- Inside CLI run these commands:
export FLASK_APP=app.py
flask run
- watch the error appear on the screen
Solution 1 :
- make the function return the app
- Clearly create a variable called app and make it equal to the return value of the function, like this:
from flask import Flask
def create_app():
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
return app
app = create_app()
Solution 2: Get the app outside of the function:
from flask import Flask
app = Flask("abc")
@app.route('/')
def hello_world():
return 'Hello, World!'
Instead of just "flask" use FLASK_APP=theflaskapp.py
, like what Marco suggested:
env FLASK_APP=theflaskapp.py python -m flask run
This should fix it, if not, make sure you are executing the command to run the script in the same directory as it. You should also check if the problem is in flask or not by running "python theflaskapp.py" (In the same directory as the flask app still) and see if it works at all.