且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Flask和uWSGI - 无法加载应用程序0(mountpoint ='')(可找不到调用或导入错误)

更新时间:2023-11-20 17:21:40

uWSGI doesn不会载入你的应用程序为 __ main __ ,所以它永远不会找到 app 以名称 __ main __ )运行。因此,如果__name__ ==__main __:块,则需要将其导入到之外。



真的很简单更改:

pre $ 从应用导入应用

if __name__ ==__main__:
app.run()

现在,您可以直接使用 python run .py 或者通过uWSGI运行它。


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

Here is my directory structure:

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

Contents of /path/to/folder/run.py

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

Contents of /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 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.

Really simple change:

from app import app

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

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