简体   繁体   English

Gunicorn导入错误:在烧瓶中找不到url_for,但是本机烧瓶运行正常

[英]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. 我可以使用flask的内置网络服务器命令run()来运行我的flask应用程序,但是尝试使用gunicorn运行我的应用程序时,我收到以下导入错误。 I use the command gunicorn project.app:app from the mainfolder. 我使用主文件夹中的命令gunicorn project.app:app。

The error being thrown off from views.py is: 从views.py抛出的错误是:

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 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 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 _config.py

import os
#All the app's keys

Procfile 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? python project / app.py可以很好地运行应用程序,那么为什么gunicorn project.app:app抛出模块ImportError?

Your import for url_for needs to come before app = Flask(__name__) 您对url_for的导入需要在app = Flask(__name__)

Your app is working on flask's server because url_for is imported prior to the 您的应用正在Flask的服务器上运行,因为url_for在导入之前

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

statement. 声明。 Gunicorn is getting the application before you have imported url_for. 在导入url_for之前,Gunicorn正在获取应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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