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:

  1. Create a file called app.py
  2. 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
  1. Inside CLI run these commands:
export FLASK_APP=app.py
flask run
  1. watch the error appear on the screen

Solution 1 :

  1. make the function return the app
  2. 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.

Tags:

Python

Flask