简体   繁体   中英

Gunicorn import error: url_for not found in flask, but native flask run works fine

I can run my flask app fine using flask's built-in web server command run(), but I'm receiving the following Import error while trying to run my app using gunicorn. I use the command gunicorn project.app:app from the mainfolder.

The error being thrown off from views.py is:

from app import db, url_for ImportError: cannot import name url_for

My app is organized as follows:

mainfolder/
    Procfile
    bin
    project/
        app.py
        _config.py
        views.py
        run.py

run.py

from views import app
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)

app.py

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
app.config.from_pyfile('_config.py')
db = SQLAlchemy(app)
app.config['SECRET_KEY'], report_errors=False)

from views import *

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

_config.py

import os
#All the app's keys

Procfile

#this works fine
web: python project/run.py

#this doesn't work
web: gunicorn project.app:app

python project/app.py runs the app fine so why does gunicorn project.app:app throw off a module ImportError?

Your import for url_for needs to come before app = Flask(__name__)

Your app is working on flask's server because url_for is imported prior to the

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

statement. Gunicorn is getting the application before you have imported url_for.

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