Reputation: 67
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?
Upvotes: 1
Views: 1332
Reputation: 2168
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.
Upvotes: 1