0

I'm having trouble writing a uWSGI entry point file that will load flask-bootstrap sample as my app is not structured to start from if name == "main": I'm just trying to use the flask-bootstrap example files which has a different Flask app.py where it calls a another file to run the if name part. My current uWSGI entry point file looks like:

from myproject import app

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

to serve my app but I believe I might need to structure it differently since my app.py file is not written this way.

My Flask app.py looks like this:

import sys

sys.path.append(os.path.dirname(__name__))

from sample_application import create_app


app = create_app()

app.run(host='0.0.0.0')

I'm following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04

Lily Su
  • 11
  • 2

2 Answers2

0

Consider /home/pi/server.py (if you're setting this up on a Raspberry Pi)

from app import create_app

app = create_app()

If /etc/uwsgi/apps-enabled/server includes

chdir = /home/pi
home = /home/pi/venv  # if you're using virtualenv
module = server:app

Then uwsgi will know to load server (server.py), and expect a WSGI application in app. Flask provides a WSGI application.

And since uwsgi is doing the loading, if __name__ == '__main__': won't be true, so if server.py reads

from app import create_app

app = create_app()

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

You can either run it via uwsgi or (assuming you're using a virtual environment) via

FLASK_APP=server.py venv/bin/flask run
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
0

ok great. Thank you! I was able to figure it out. I had:

import os
import sys
from sample_application import create_app
sys.path.append(os.path.dirname(__name__))


app = create_app()

if __name__ == "__main__":
    app.run()
Lily Su
  • 11
  • 2