简体   繁体   中英

Heroku Flask - Deploy a 'modular' app from tutorial not working, foreman start works locally

based on this structure: http://flask.pocoo.org/docs/patterns/packages/

I also tried this post: Deploying Flask app to Heroku

I am having trouble getting this to work on heroku. I usually get the PORT does not set within 60 seconds error. I have read other SO posts and just can't figure out if my project structure is wrong or my procfile. I tried other ports than 5000 as well.

Here is my current project structure:

/myapplication
    Procfile
    runserver.py
    /applicationfolder
        __init__.py
        views.py

Here is my Procfile

web: python runserver.py $PORT

Here is my runserver.py

from applicationfolder import app
app.run()

if __name__ == '__main__':
    import os
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

Here is my init .py

import os

from flask import Flask
from flask import render_template, jsonify, request

app = Flask(__name__)

app.config.from_object('config')

import applicationfolder.views

From there views.py runs.

This works locally with foreman start and python runserver.py, but does not work with heroku. I have tried many things with PORT but port doesn't seem to set even with a different PORT than 5000. I think it has something to do with my project structure.

The app.run() was in there twice, which as you noted is what's screwing things up. The app.run() invokes a simply pure-python development server so that you can easily run and/or debug your script.

By invoking it at the module level (right under your import in runserver.py), you were effectively trying to start the development server as the python code was loaded, and then when it went to run it when invoked from the Procfile, the development server was already in flight, having been starting with it's defaults (latest version of Flask is pulling relevant defaults from the SERVER_NAME environment variable). By having it in both places, you were trying to invoke that method twice.

You basically want either the straight up module load (in which case, kill off the code under "if name ...", or you use the code when invoking under main , in which case don't start the service at module load time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM