Flask 和 uWSGI - 无法加载应用程序 0 (mountpoint='')(找不到可调用或导入错误) [英] Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)

查看:50
本文介绍了Flask 和 uWSGI - 无法加载应用程序 0 (mountpoint='')(找不到可调用或导入错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get the below error when I try and start Flask using uWSGI. Here is how I start:

>  # 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)

推荐答案

uWSGI 不会将您的应用加载为 __main__,因此它永远不会找到 app(因为仅在应用以名称 __main__ 运行时才会加载).因此,您需要在 if __name__ == "__main__": 块之外导入它.

uWSGI doesn't load your app as __main__, so it never will find the app (since that only gets loaded when the app is run as name __main__). Thus, you need to import it outside of the if __name__ == "__main__": block.

非常简单的改变:

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

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

现在您可以直接使用 python run.py 运行该应用程序,或者以您拥有的方式通过 uWSGI 运行它.

Now you can run the app directly with python run.py or run it through uWSGI the way you have it.

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

NOTE: if you set --callable myapp, you'd need to change it from as application to myapp (by default uwsgi expects application

这篇关于Flask 和 uWSGI - 无法加载应用程序 0 (mountpoint='')(找不到可调用或导入错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆