Flask / Python error: ModuleNotFoundError: No Module name 'models'

Hello Everybody

I’m working through a tutorial for making a database connection with Mariadb (xampp) and for most part I think I have it working except for the error message I keep getting. My Flask/python is still a bit limited and after two weeks of Googling its time to reach out.

I believe the problem is within user.py or maybe in the init.py the script isn’t able to see the models.py to make the communication with the Member model, I think.

What am I missing?

folder/file structure

Error message

File “D:\xampp\htdocs\flaskLearning\learning_init_.py”, line 24, in create_app from .user import user
File “D:\xampp\htdocs\flaskLearning\learning\user\user.py”, line 5, in from models import Members
ModuleNotFoundError: No module named ‘models’

config.py

class Config:     
    TESTING = False 
    STATIC_FOLDER = 'static'
    TEMPLATES_FOLDER = 'templates'    
    DATABASE_URI = 'mariadb+mariadbconnector://DB_USERNAME1:DB_PASSWORD@DB_HOST/DB_NAME", echo = False, future = True'
   

'init.py

import os
from flask import Flask, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from config import Config

def create_app(app_config=None):
    app = Flask(__name__, instance_relative_config=True)    
   
    if app_config is None:        
        app.config.from_object('config.DevConfig')        
    else:
        app.config.from_mapping(app_config)

    # Register Blueprints 
    from .dashboard import dashboard
    from .user import user    

    app.register_blueprint(user.userBP, url_prefix='/')
    app.register_blueprint(dashboard.dashboardBP, url_prefix='/') 

    **# problem area: im not sure if i need this line. if needed not sure if i did it correctly**  
    from .models import Members  <-- possible problem **

models.py

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date

Base = declarative_base()

class Members(Base):
    __tablename__ = 'tbl_users'
    usr_index = Column(Integer, primary_key=True)
    usr_email = Column(String(length=100), unique=True)

user.py

from flask import Blueprint, render_template, request, redirect, url_for, flash
from sqlalchemy import create_engine 
from sqlalchemy.orm import sessionmaker
from config import Config

# problem area: communication with the Member model isn't visible
**from models import Members**  <- problem area

userBP = Blueprint('user', __name__, url_prefix='/user', template_folder="templates", 
                    static_folder = 'static')

engine = create_engine(Config.DATABASE_URI)
if engine:
    print("DB testing =>: Connected!")
else:
    print("DB testing => NOT Connected!")
# Base.metadata.create_all(engine)

################## USER AUTHENTICATION ####################### 
@userBP.route('/login', methods = ['GET', 'POST']) 
def login():    
    if request.method == "POST":
                if testEmail == inp_email and testPWD == inp_pwd:
                # testFind = Members().query.filter_by(usr_email=email)
                # Testing DB connectionCreate a session
                Session = sessionmaker()                
                Session = sessionmaker(bind=engine)
                session = Session()

                **# problem area: Members isnt available to the query statement**
                membersList = session.query(Members).all()
1 Like

It’s not in the same directory. You’ll either have to put the module in the same directory as the script thats calling it, or import sys and start modifying the sys.path so that python can find the file.