Search code examples
pythonflaskheroku

How can I push my Flask_socketio app correctly to Heroku?


I've been trying to push my Flask_socketio app to Heroku, but Heroku can't find my app by Procfile, probably because of my package structure. How can I restructure my directory? Or, Should I change my Procfile. Thanks in advance.

My directory structure:

|App
| |-templates
| |-main
| |-__init__.py
|app.py
|requirements.txt
|Procfile

App.py:

from app import create_app, socketio

app = create_app(debug=True)

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

app/init.py:

from flask import Flask
from flask_socketio import SocketIO

socketio = SocketIO()

def create_app(debug=False):
    "Create an application."

    app = Flask(__name__)
    app.debug = debug
    app.config['SECRET_KEY'] = 'MRHBLR'

    from .main import main 
    app.register_blueprint(main)

    socketio.init_app(app)
    return app

Procfile:

web: gunicorn --worker-class eventlet -w 1 app:app

Heroku app's log:

2021-04-03T19:34:00.216344+00:00 app[web.1]: Failed to find attribute 'app' in 'app'.
2021-04-03T19:34:00.217370+00:00 app[web.1]: [2021-04-03 19:34:00 +0000] [10] [INFO] Worker exiting (pid: 10)
2021-04-03T19:34:00.248331+00:00 app[web.1]: [2021-04-03 19:34:00 +0000] [4] [WARNING] Worker with pid 10 was terminated due to signal 15
2021-04-03T19:34:00.345570+00:00 app[web.1]: [2021-04-03 19:34:00 +0000] [4] [INFO] Shutting down: Master
2021-04-03T19:34:00.345660+00:00 app[web.1]: [2021-04-03 19:34:00 +0000] [4] [INFO] Reason: App failed to load.

Solution

  • You seem to have two things called app in your top-level, right? You have the app.py file and the app package. That is not going to work, try renaming one of them. For example, change app.py to myapp.py, then pass myapp:app in the Gunicorn command line.