How can I run the main file with flask?

Question:

I am trying to run the server (it’s about oauth2):

The main file looks like this:

if __name__ == '__main__':
    from flask import Flask
    app = Flask(__name__)
    app.debug = True
    app.secret_key = 'development'
    app.config.update({
        'SQLALCHEMY_DATABASE_URI': 'sqlite:///test.sqlite'
    })
    app = create_server(app)
    app.run()

However, I am getting this error:

Error: Failed to find Flask application or factory in module ‘hello’. Use ‘FLASK_APP=hello:name’ to specify one.

I executed the following commands in terminal:

export FLASK_APP=server.py` and 
export FLASK_APP=main.py

After that, I tried rerunning with flask run

Again, I am getting this error:

Error: Failed to find Flask application or factory in module ‘main’. Use ‘FLASK_APP=main:name’ to specify one.

Asked By: information

||

Answers:

Try this code

from flask import Flask

app = Flask(__name__)
app.debug = True
app.secret_key = 'development'
app.config.update({
   'SQLALCHEMY_DATABASE_URI': 'sqlite:///test.sqlite'
})

if __name__ == '__main__':
    app.run(host="0.0.0.0", port="5000", debug=True)
Answered By: Elie Saad

Try this one:

But I recommend you to start with the basics before work with databases. Try to render HTML templates, use bootstrap, etc. You are running here. Anyway, this boilerplate works for me.

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

app = Flask(__name__)
# Key for Forms
app.config['SECRET_KEY'] = 'mysecretkey'


# SQL DATABASE AND MODELS
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
Migrate(app, db)


class Puppy(db.Model):

    __tablename__ = 'puppies'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text)

    def __init__(self, name):
        self.name = name


@app.route('/')
def index():
    return '<h1>Hello Puppy!</h1>'


if __name__ == '__main__':
    app.run(debug=True)

You should run it directly

python server.py

And if you want to use flask run then you would have to put all (except app.run()) before if __name__ == '__main__': because flask run will import this file and import will skip code inside if __name__ == '__main__':

# ... other code ...

from flask import Flask
app = Flask(__name__)
app.debug = True
app.secret_key = 'development'
app.config.update({
    'SQLALCHEMY_DATABASE_URI': 'sqlite:///test.sqlite'
})
app = create_server(app)

if __name__ == '__main__':
    app.run()

And it will need export FLASK_APP=server:app because you want to run file server.py and use instance of Flask() with name app

export FLASK_APP=server:app

flask run

Because code uses standard name app so you could skip it in export

export FLASK_APP=server

flask run

You can also run without export

flask --app server run
Answered By: furas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.