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

在CMD中运行Flask Shell时出现ModuleNotFoundError: No module named 'main'的原因排查求助

ModuleNotFoundError: No module named 'main' in Flask Shell

Let's break down why you're hitting this error and how to fix it step by step:

1. Incorrect Import Path in app/__init__.py

Your current import line from main import main_blueprint assumes main is a top-level Python module, but in your project structure, main is a sub-package nested inside the app directory. This mismatch is the root cause of the module not found error.

To fix this, use one of these valid import styles:

  • Relative import (best for intra-package references, since we're importing from a sibling module within the app package):
    from .main import main_blueprint
    
  • Absolute import (works if your project root is added to Python's system path):
    from app.main import main_blueprint
    

2. Python Path & FLASK_APP Environment Variable Misconfiguration

When running flask shell, Flask needs to locate your application package correctly. If your project root directory isn't in Python's sys.path, it can't resolve the app.main module properly.

Fix steps:

  • First, make sure you're running the flask shell command from your project root directory (the folder containing app/, static/, and templates/).
  • Set the FLASK_APP environment variable to point to your app package:
    • On Windows CMD:
      set FLASK_APP=app
      
    • On Linux/macOS:
      export FLASK_APP=app
      
  • Run flask shell again after setting the variable.

3. Double-check Blueprint Registration (Optional but Critical)

While this isn't the direct cause of your current error, once you fix the import, ensure you're registering the blueprint in your app/__init__.py (either directly or in an app factory):

from flask import Flask
from .main import main_blueprint

# If not using an app factory
app = Flask(__name__)
app.register_blueprint(main_blueprint)

# If using an app factory (recommended for larger apps)
def create_app():
    app = Flask(__name__)
    app.register_blueprint(main_blueprint)
    # Add other configs, extensions here
    return app

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 13:08:13