73

当我尝试使用 uWSGI 启动 Flask 时出现以下错误。我是这样开始的:

>  # cd ..
>     root@localhost:# uwsgi --socket 127.0.0.1:6000 --file /path/to/folder/run.py --callable app -  -processes 2

这是我的目录结构:

-/path/to/folder/run.py
      -|app
          -|__init__.py
          -|views.py
          -|templates
          -|static

的内容/path/to/folder/run.py

if __name__ == '__main__':
   from app import app
   #app.run(debug = True)
   app.run()

的内容/path/to/folder/app/__init__.py

import os
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
#from flaskext.babel import Babel
from config import basedir
app = Flask(__name__)
app.config.from_object('config')
#app.config.from_pyfile('babel.cfg')

db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.setup_app(app)
login_manager.login_view = 'login'
login_manager.login_message = u"Please log in to access this page."

from app import views

*** Operational MODE: preforking ***
unable to find "application" callable in file /path/to/folder/run.py
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (pid: 26972, cores: 1)
spawned uWSGI worker 2 (pid: 26973, cores: 1)
4

3 回答 3

198

我对接受的解决方案有疑问,因为我的烧瓶应用程序位于一个名为app. 你可以通过把这个放在你的wsgi中来解决这个问题:

from module_with_your_flask_app import app as application

所以问题只是 uwsgi 需要一个名为application.

于 2015-08-17T22:23:07.703 回答
52

uWSGI 不会将您的应用程序加载为__main__,因此它永远不会找到app(因为只有在应用程序以 name 运行时才会加载__main__)。因此,您需要将其导入if __name__ == "__main__":块之外。

非常简单的改变:

from app import app as application  # for example, should be app

if __name__ == "__main__":
    application.run()

现在,您可以直接使用python run.py或通过 uWSGI 运行应用程序,就像您拥有它一样。

注意:如果您设置--callable myapp,您需要将其从 更改as applicationmyapp(默认情况下uwsgi期望application

于 2012-08-19T23:46:04.850 回答
2

unable to load app 0 (mountpoint='') (callable not found or import error)如果我遗漏了以下Flask 应用程序最小工作示例的最后两行,则会发生uWSGI 错误

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello world!"

if __name__ == "__main__":
    app.run()
else:
    application = app

我知道这已经在对另一个答案的评论中含蓄地说,但我仍然需要一段时间才能弄清楚,所以我希望节省其他人的时间。

对于纯Python Dash 应用程序,我可以提供以下最小可行代码片段:

import dash
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
app.layout = html.Div( html.H1(children="Hello World") )

application = app.server

if __name__ == "__main__":
    app.run_server(debug=True)

同样,application = app.server这里是必不可少的部分。

于 2019-07-19T11:13:27.500 回答