You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用uWSGI运行Flask应用报错:AttributeError: module 'myApp.app' has no attribute 'run'

问题:Flask应用工厂模式下uWSGI启动报错AttributeError: module 'myApp.app' has no attribute 'run'

我有一个采用Blueprints的Flask应用,运行在虚拟环境(venv)中。应用结构如下:

myApp
├── app
│   ├── about
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── extensions.py
│   ├── __init__.py
│   ├── main
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── models
│   │   ├── contact.py
│   ├── static
│   │   ├── myApp.css
│   │   ├── favicon-16x16.png
│   └── templates
│       ├── about
│       │   └── index.html
│       ├── base.html
│       └── index.html
├── bin
│   ├── activate
│   ├── flask
│   ├── pip
│   ├── python -> python3.12
│   ├── python3 -> python3.12
│   ├── python3.12 -> /usr/bin/python3.12
│   ├── uwsgi
│   └── wheel
├── config.py
├── myApp.db
├── myApp -> /home/janeDoe/myApp
├── myApp.ini
├── include
│   └── ...
├── lib
│   └── ...
├── lib64 -> lib
├── pyvenv.cfg
├── requirements.txt
└── wsgi.py

执行flask run命令时页面可正常渲染,但使用命令uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app启动时,出现错误提示AttributeError: module 'myApp.app' has no attribute 'run'。

当前wsgi.py代码:

from myApp import app
app.run()

app/init.py代码:

from flask import Flask
from config import Config
from app.extensions import db
from flask_favicon import FlaskFavicon

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    # Initialize Flask extensions here
    db.init_app(app)

    # Register blueprints here
    from app.main import bp as main_bp

    app.register_blueprint(main_bp)

    from app.about import bp as about_bp

    app.register_blueprint(about_bp, url_prefix="/about")

    flaskFavicon = FlaskFavicon()
    flaskFavicon.init_app(app)
    flaskFavicon.register_favicon("app/static/favicon-16x16.png", "default")

    @app.route("/test/")
    def test_page():
        return "<h1>Testing the Flask Application Factory Pattern</h1>"

    return app

完整错误信息:

...

mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "/home/JaneDoe/myApp/wsgi.py", line 5, in <module>
    app.run()
    ^^^^^^^
AttributeError: module 'myApp.app' has no attribute 'run'
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 (and the only) (pid: 1321333, cores: 1)

我本以为调用create_app时会从Flask导入app.run(),请问需要修改什么才能让app.run()被识别?


解决方案

问题根源

你在wsgi.py中导入的myApp.app是Python模块,而非Flask应用实例。因为采用了应用工厂模式(通过create_app函数创建实例),模块本身并没有直接暴露Flask app对象,自然没有run方法。另外,uWSGI作为生产服务器,不需要调用Flask自带的app.run()(这是开发服务器的启动方式),只需要你提供一个可调用的Flask实例即可。

修改步骤

  1. 修正wsgi.py,正确创建并导出Flask实例
    将wsgi.py内容替换为:
from myApp.app import create_app

# 创建Flask应用实例
app = create_app()

# 仅在直接运行wsgi.py时启动开发服务器,uWSGI会直接使用app变量
if __name__ == "__main__":
    app.run()
  1. 确保应用工厂可被正确导入(可选)
    如果希望从myApp直接导入create_app,可以在myApp/__init__.py中添加:
from app import create_app

这样wsgi.py也可以写成from myApp import create_app,效果一致。

  1. 重新启动uWSGI
    保持原命令不变即可:
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

补充说明

flask run能正常工作是因为Flask CLI会自动识别并调用应用工厂函数create_app,这是Flask的默认约定;而uWSGI不会自动处理应用工厂,必须由你显式创建实例并导出给它使用。

内容的提问来源于stack exchange,提问作者martin1959

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 04:53:12