KexAri KexAri - 16 days ago 6
Python Question

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

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?

Answer

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.