OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

ModuleNotFoundError message using Python

  • Thread starter Thread starter itzick binder
  • Start date Start date
I

itzick binder

Guest
I'm trying to creat an API using Python. In my app folder there are the folders: models, services, tests. In each folder (including app) there is an empty init.py

In my model folder there is Ad.py file.

In my services folder there is an AdService.py file.

This is part of the code in my AdService.py file:

Code:
from models.Ad import Ad
from models.SubCategory import SubCategory
from database import session


class AdService:
    .
    .
    .

In my tests folder there is a file name AdServiceTest.py.

This is part of the code in my AdServiceTest.py file:

Code:
import unittest
from datetime import datetime
from app.models.Ad import Ad  # Ensure the import path is correct
from app.services.AdService import AdService  # Ensure the import path is correct
from app.database import (Base,engine,session)  # Assuming Base and engine are defined in your database module
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import create_engine

# Setup in-memory SQLite database for testing
DATABASE_URL = "XXXX"
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
session = scoped_session(Session)


class AdServiceTest(unittest.TestCase):
.
.
.

When I run my AdServiceTest.py I get an error on the line: "from app.models.Ad import Ad" with the text: "ModuleNotFoundError: No module named 'app'".

I tried changing the prefix to just models.Ad or ..models.Ad and nothing worked.

What am I doing wrong?

Thank you in advance!
<p>I'm trying to creat an API using Python. In my app folder there are the folders: models, services, tests. In each folder (including app) there is an empty <strong>init</strong>.py</p>
<p>In my model folder there is Ad.py file.</p>
<p>In my services folder there is an AdService.py file.</p>
<p>This is part of the code in my AdService.py file:</p>
<pre><code>from models.Ad import Ad
from models.SubCategory import SubCategory
from database import session


class AdService:
.
.
.
</code></pre>
<p>In my tests folder there is a file name AdServiceTest.py.</p>
<p>This is part of the code in my AdServiceTest.py file:</p>
<pre><code>import unittest
from datetime import datetime
from app.models.Ad import Ad # Ensure the import path is correct
from app.services.AdService import AdService # Ensure the import path is correct
from app.database import (Base,engine,session) # Assuming Base and engine are defined in your database module
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import create_engine

# Setup in-memory SQLite database for testing
DATABASE_URL = "XXXX"
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
session = scoped_session(Session)


class AdServiceTest(unittest.TestCase):
.
.
.
</code></pre>
<p>When I run my AdServiceTest.py I get an error on the line: "from app.models.Ad import Ad" with the text: "ModuleNotFoundError: No module named 'app'".</p>
<p>I tried changing the prefix to just models.Ad or ..models.Ad and nothing worked.</p>
<p>What am I doing wrong?</p>
<p>Thank you in advance!</p>
 

Latest posts

Online statistics

Members online
1
Guests online
6
Total visitors
7
Top