Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't need the run() method in a Flask application?

Tags:

python

flask

I have a Flask application setup on my linode with a directory structure like so:

|--------flask-test
|----------------app
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py
|-----------------------main.py

my __init__.py is:

# __init__.py
from flask import Flask
from main import main
app = Flask(__name__)
app.register_blueprint(main)
app.run()

and main.py like so:

# main.py
from flask import Blueprint

main = Blueprint('main',__name__)
@main.route("/")
def hello():
    return "Hello World!"

@main.route("/england/")
def england():
    return "Hello England!"

If I run the app locally there are no issues. If I go to my server address in the web browser I get an internal server error. However if I remove the line: app.run from __init__.py it works fine. Why is this? Why do I not need the run method?

like image 571
KexAri Avatar asked Dec 15 '22 02:12

KexAri


1 Answers

You should do

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

The reason is that Apache or NGINX or some other web server loads your app directly on the server but app.run() runs flask's internal web-server so you can test your app.

like image 81
Cfreak Avatar answered Jan 02 '23 04:01

Cfreak