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

运行app/__init__.py时出现NameError: name 'app' is not defined,求帮助

Fixing the NameError: name 'app' is not defined in your Flask-RESTful app

Hey there! Let's break down exactly why you're hitting this error and fix it step by step—since you're new to Flask, I'll explain each issue clearly so you understand what's going on.

Let's go through the problems one by one:

  • 1. Typo in the __main__ guard clause
    Your current code has a syntax error here:

    if__name__== '___main___':
    

    There are two mistakes:

    • Missing spaces around if, == (Python is picky about spacing!)
    • ___main___ uses three underscores on each side, but it should be two underscores: __main__
      Correct version:
    if __name__ == '__main__':
    
  • 2. You never created an app instance in the main block
    The app variable is defined inside your create_app() function, but you never actually call this function to generate the app instance. You need to call create_app() and assign it to app before trying to run it:

    if __name__ == '__main__':
        app = create_app()
        app.run(port=8000, debug=True)
    
  • 3. Incorrect import and resource registration
    In your create_app function, you're importing rest3_product from resource.rest3_product, but the actual resource class in that file is plyPosition. Change the import line to:

    from resource.rest3_product import plyPosition
    

    This way, api.add_resource(plyPosition, "/plyPosition") references the correct class you defined.

  • 4. Undefined initialize_extensions function
    You're calling initialize_extensions(app) but haven't written this function anywhere. If you don't have any extensions (like SQLAlchemy) to set up yet, either comment out this line or add a placeholder function:

    def initialize_extensions(app):
        # Add extension setup here later (e.g., db.init_app(app))
        pass
    

Corrected app/__init__.py code:

from flask import Flask
from flask_restful import Api

def initialize_extensions(app):
    # Placeholder for future extensions
    pass

def create_app(config_name='development'):
    app = Flask(__name__)
    api = Api(app)
    # Import the correct resource class
    from resource.rest3_product import plyPosition
    api.add_resource(plyPosition, "/plyPosition")
    initialize_extensions(app)
    return app

if __name__ == '__main__':
    # Create the app instance first
    app = create_app()
    app.run(port=8000, debug=True)

Minor cleanup for rest3_product.py:

You don't need to re-import Flask and Api here—they're already initialized in the create_app function. Simplify the imports to avoid redundancy:

from flask_restful import Resource, reqparse

class plyPosition(Resource):
    def post(self):
        parser = reqparse.RequestParser(bundle_errors=True)
        parser.add_argument("productId", required=True, type=int)
        parser.add_argument("productCatId", required=True, type=int)
        parser.add_argument("measureUniId", required=True, type=int)
        parser.add_argument("productName", required=True, type=str)
        parser.add_argument("length", required=True, type=int)
        parser.add_argument("breadth", required=True, type=int)
        parser.add_argument("height", required=True, type=int)
        parser.add_argument("createdAt", required=True, type=int)
        parser.add_argument("updatedAt", required=True, type=int)
        try:
            args = parser.parse_args()
        except Exception:
            return "Please Check Your Request!!!!!!!!!"
        return args

Why this works:

  • The create_app() function now properly creates and returns the Flask app instance
  • The main block calls create_app() to get the app variable, so it's no longer undefined
  • All imports and resource registrations point to the correct class
  • The undefined function issue is resolved with a placeholder you can build on later

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:08:57