Home > other >  Django ModuleNotFoundError: No module named 'models'
Django ModuleNotFoundError: No module named 'models'

Time:05-30

I'm building a web app in Django to purchase movie tickets. All the logic handling the database is in the 'ticket_handler' app. The actual website is in the 'movie_site' app and I'm building a restful API in the 'ticketing_api' app.

Whenever I try to import something from one app to another, I get a "ModuleNotFound" error. Here is my directory structure:

Movie_Ticketing_Service
|- movie_server
|   |- movie_server
|   |   |- __init__.py
|   |   |- asgi.py
|   |   |- settings.py
|   |   |- urls.py
|   |   |- wsgi.py
|   |- movie_site
|   |   |- migrations
|   |   |- templates
|   |   |- __init__.py
|   |   |- admin.py
|   |   |- apps.py
|   |   |- forms.py
|   |   |- models.py
|   |   |- tests.py
|   |   |- urls.py
|   |   |- views.py
|   |- ticket_handler
|   |   |- migrations
|   |   |- __init__.py
|   |   |- admin.py
|   |   |- apps.py
|   |   |- billing_handler.py
|   |   |- mail_handler.py
|   |   |- models.py
|   |   |- tests.py
|   |   |- ticket_handler.py
|   |   |- views.py
|   |- tecketing_api
|   |   |- migrations
|   |   |- __init__.py
|   |   |- admin.py
|   |   |- apps.py
|   |   |- forms.py
|   |   |- models.py
|   |   |- tests.py
|   |   |- views.py
|   |- db.sqlite3
|   |- manage.py
|- venv

This is my installed apps in settings.py:

 INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'movie_site.apps.MovieSiteConfig',
    'ticket_handler.apps.TicketHandlerConfig',
    'ticketing_api.apps.TicketingApiConfig'
]

The code that's throwing the exception is the import statements in movie_site/views.py:

from django.shortcuts import render, redirect
from ticket_handler.models import Movie, Showing
from ticket_handler.ticket_handler import TicketHandler
from ticket_handler.billing_handler import BillingHandler
from forms import TicketsForm, PurchaseForm, NewUserForm
from django.contrib.auth import login
from django.contrib import messages

it gives me: ModuleNotFoundError: No module named 'models'

I originally tried:

from movie_server.ticket_handler.models import Movie, Showing
from movie_server.ticket_handler.ticket_handler import TicketHandler
from movie_server.ticket_handler.billing_handler import BillingHandler

PyCharm likes that better, but I get: ModuleNotFoundError: No module named 'movie_server.ticket_handler'

I'm kind of tearing my hair out over this and googling hasn't cleared it up for me. Let me know if you need any more information!

Edit: I was asked for the traceback, so here it is:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\core\management\commands\runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\core\management\base.py", line 487, in check
    all_issues = checks.run_checks(
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\core\checks\urls.py", line 42, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\core\checks\urls.py", line 61, in _load_all_namespaces
    url_patterns = getattr(resolver, "url_patterns", [])
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\utils\functional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\urls\resolvers.py", line 696, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\utils\functional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\XXX\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\urls\resolvers.py", line 689, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\Devon\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\Devon\PycharmProjects\Movie_Ticketing_Service_2\movie_server\movie_server\urls.py", line 20, in <module>
    path('', include('movie_site.urls')),
  File "C:\Users\Devon\PycharmProjects\Movie_Ticketing_Service_2\venv\lib\site-packages\django\urls\conf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\Devon\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\Devon\PycharmProjects\Movie_Ticketing_Service_2\movie_server\movie_site\urls.py", line 3, in <module>
    from . import views
  File "C:\Users\Devon\PycharmProjects\Movie_Ticketing_Service_2\movie_server\movie_site\views.py", line 3, in <module>
    from ticket_handler.ticket_handler import TicketHandler
  File "C:\Users\Devon\PycharmProjects\Movie_Ticketing_Service_2\movie_server\ticket_handler\ticket_handler.py", line 3, in <module>
    from models import Showing, Ticket
ModuleNotFoundError: No module named 'models'

Hopefully that helps!

CodePudding user response:

From your traceback I noticed you forgot to add the dot before models:

Change

from models import Showing, Ticket

to

from .models import Showing, Ticket
  • Related