Search code examples
flaskherokuwaitress

Python Flask Web application not able to deploy on Heroku using Waitress server


I am trying to deploy my Flask application on Heroku PaaS Cloud Service. It, however, works on localhost with no errors. I am using git for version control and pushing to Heroku. Initially, as a web server, I used 'gunicorn' but later came to know it is useful for UNIX distributions. So I resorted my webserver to 'waitress'. I saw some of the posts and tried everything in the Procfile for hosting to Heroku. My Procfile reads like: web: waitress-serve --port=$PORT app:app. I also know that the first app in this line is the package and the second app is the name of the instance best to my knowledge. I even changed from app to myapp, website and I have all the packages including these mentioned are in my VS code editor. But my application is not getting deployed onto Heroku and it gives Application Error. Now when I check the logs using heroku logs --tail it gives me the following error as in the screenshot. Any help would be highly obliged. I am trying this for 23 hours. Heroku Logs


Solution

  • The Procfile looks correct but the problem could be that the file (app.py) and the variable (app) have the same name.

    I suggest the following approach, in app.py

    # define Flask app
    def create_app():
      try:
    
        web_app = Flask(__name__)
    
        logging.info('Starting up..')
    
        return web_app
    
      except Exception as e:
        logging.exception(e)
    
    # retrieve port
    def get_port():
      return int(os.environ.get("PORT", 5000))
    
    # start Flask app
    if __name__ == '__main__':
      web_app = create_app()
    
      web_app.run(debug=False, port=get_port(), host='0.0.0.0')
    

    The application can be launched from the Procfile with

    web: waitress-serve --port=$PORT --call 'app:create_app'