0

in sub folder, I am coding a python file(/test_utils/test_manager.py).

import csv
from models import TestQuestions
from database import db
from main import serendipity_app

And in that file, I am using modules(database, models) in above folder. But when I execute main.py with 'python main.py' python said ModuleNotFoundError: No module named 'models'.

I don't understand why this error occured.

the image below is my project structure.

enter image description here

Kazaka Nelson
  • 158
  • 3
  • 14

1 Answers1

0

Assuming you're working on file test_manager.py, import models like, from projectName.models import TestQuestions. make sure you're using the correct path for models directory from . import models works, but import models doesn't

Siddaram H
  • 1,126
  • 12
  • 17
  • I've updated the answer.. if this won't work, share some sample code of models file, issue could be something different..I'm assuming TestQuestioins/db are the classes in that models/database files – Siddaram H May 15 '18 at 19:24
  • class TestQuestions(db.Model): __tablename__ = 'test_questions' id = db.Column(db.Integer(), autoincrement=True, primary_key=True) test_id = db.Column(db.Integer(), nullable=False) question_number = db.Column(db.Integer(), nullable=False) question_string = db.Column(db.String(500), nullable=False) question_tag = db.Column(db.String(10), nullable=True) is_reverse_scoring = db.Column(db.Boolean, default=False) – Kazaka Nelson May 17 '18 at 01:33
  • https://stackoverflow.com/questions/4383571/importing-files-from-different-folder try the answers in these.. – Siddaram H May 17 '18 at 06:24
  • I just tried to import os, sys. and then sys.path.append etc. and then. I got other errors. It's ridiculous. Even a file I didn't import run... – Kazaka Nelson May 17 '18 at 17:19