azro
azro

Reputation: 54148

Deploy a Flask app with gunicorn (exploreflask tuto)

To understand how to deploy a Flask app, I read some tutos and found the one of exploreflask quite good.

I used the blueprints-functional structure to build my test-app, BUT at the end, at the deployment part with an application runner like gunicorn, the tuto uses a example with one unique file, quite different of the rest

arandomfoldername/
    config.py
    requirements.txt
    run.py
    instance/
      config.py
    myappname/
        __init__.py
        static/
        templates/
            home/
            control/
        views/
            __init__.py
            home.py
            control.py
        models.py

And

# myappname/__init__.py
from flask import Flask
from .views.home import bluehome 
from .views.control import bluecontrol 

app = Flask(__name__, instance_relative_config=True)
app.register_blueprint(bluehome)
app.register_blueprint(bluecontrol)

So I tried gunicorn myappname:app but I got a No module named myappname, the app variable is in the __init_.py in the myappname package (as the tuto shows in the blueprints part)

I used this flask tuto to build the wheel file

  1. How to manage and fix this ?

  2. How the parameters given in the top-file config.py are supposed to utile as they are not used in the wheel file ?

Upvotes: 0

Views: 804

Answers (1)

azro
azro

Reputation: 54148

In somes tutos, like flask.pocoo it says to build a wheel file and install it, but gunicorn doesn't use this way.

  • use the Factory pattern
  • use a file named for ex wsgi.py to import the app from the factory

Then copy the whole folder in the production venv:

arandomfoldername/
    config.py
    requirements.txt
    run.py
    instance/
      config.py
    myappname/
        __init__.py
        wsgi.py
        static/
        templates/
            home/
            control/
        views/
            __init__.py
            home.py
            control.py
        models.py

And then

cd arandomfoldername
gunicorn myappname.wsgi:app

Upvotes: 1

Related Questions