Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)

本文介绍了一种常见的uWSGI环境下Flask应用无法正确加载的问题,并提供了解决方案。主要原因是uWSGI不以__main__方式运行应用,导致找不到Flask实例。通过将Flask应用实例的导入移出if __name__ == '__main__':块,即可解决此问题。

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)
share improve this question
 

2 Answers

up vote 13down voteaccepted

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 i

*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** python threads support enabled your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 416880 bytes (407 KB) for 8 cores *** Operational MODE: preforking+threaded *** uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI master process (pid: 1893) spawned uWSGI worker 1 (pid: 1894, cores: 2) spawned uWSGI worker 2 (pid: 1895, cores: 2) spawned uWSGI worker 3 (pid: 1896, cores: 2) spawned uWSGI worker 4 (pid: 1897, cores: 2) uwsgi_master_fifo()/unlink(): Is a directory [core/fifo.c line 108] uwsgi_master_fifo()/mkfifo(): File exists [core/fifo.c line 112] ModuleNotFoundError: No module named 'main' ModuleNotFoundError: No module named 'main' unable to load app 0 (mountpoint='') (callable not found or import error) unable to load app 0 (mountpoint='') (callable not found or import error) ModuleNotFoundError: No module named 'main' ModuleNotFoundError: No module named 'main' unable to load app 0 (mountpoint='') (callable not found or import error) unable to load app 0 (mountpoint='') (callable not found or import error) WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1a9b460 pid: 1897 (default app) WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1a9b460 pid: 1894 (default app) WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1a9b460 pid: 1896 (default app) WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1a9b460 pid: 1895 (default app) Wed May 28 15:24:00 2025 - uWSGI worker 4 error: the master disconnected from this worker. Shutting down the worker. Wed May 28 15:24:00 2025 - uWSGI worker 4 error: the master disconnected from this worker. Shutting down the worker. Wed May 28 15:24:00 2025 - uWSGI worker 3 error: the master disconnected from this worker. Shutting down the worker. Wed May 28 15:24:00 2025 - uWSGI worker 2 error: the master disconnected from this worker. Shutting down the worker. Wed May 28 15:24:00 2025 - uWSGI worker 1 error: the master disconnected from this worker. Shutting down the worker. Wed May 28 15:24:00 2025 - uWSGI worker 3 error: the master disconnected from this worker. Shutting down the worker. Wed May 28 15:24:00 2025 - uWSGI worker 1 error: the master disconnected from this worker. Shutting down the worker. Wed May 28 15:24:00 2025 - uWSGI worker 2 error: the master disconnected from this worker. Shutting down the worker.
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值