运行app/__init__.py时出现NameError: name 'app' is not defined,求帮助
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__':- Missing spaces around
2. You never created an
appinstance in the main block
Theappvariable is defined inside yourcreate_app()function, but you never actually call this function to generate the app instance. You need to callcreate_app()and assign it toappbefore trying to run it:if __name__ == '__main__': app = create_app() app.run(port=8000, debug=True)3. Incorrect import and resource registration
In yourcreate_appfunction, you're importingrest3_productfromresource.rest3_product, but the actual resource class in that file isplyPosition. Change the import line to:from resource.rest3_product import plyPositionThis way,
api.add_resource(plyPosition, "/plyPosition")references the correct class you defined.4. Undefined
initialize_extensionsfunction
You're callinginitialize_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 theappvariable, 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

