ModuleNotFoundError: No module named 'school.message_test'

64 Views Asked by At

I'm encountering a ModuleNotFoundError when trying to import a module in my Django project. Here's the code snippet:

import os
import django
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from django.core.asgi import get_asgi_application
from school.message_test.routing import websocket_urlpatterns

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'school.settings')
django.setup()

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
        URLRouter(
            websocket_urlpatterns
        )
    ),
})

specifically on this line:

from school.message_test.routing import websocket_urlpatterns

I've double-checked that all the paths are correct, and the file is in the right directory. What could be causing this issue, and how can I resolve it? to run the project I used this command:

daphne -p 8000 school.asgi:application

I'll try to use a relative path which naturally won't work. I also tried using it for the top level, that is

from ..message_test.routing import websocket_urlpatterns

but it doesn't work because asgi doesn't allow it the path is absolutely correct, what can you tell me? photo of the full project path also the code from message_test/routing.py

# routing.py

from django.urls import re_path
from school.message_test.consumers.chat_consumer import ChatConsumer

websocket_urlpatterns = [
    re_path(r'ws/chat/(?P<room_name>\w+)/$', ChatConsumer.as_asgi()),
]

also the code from the root of the routing project

# school/routing.py

from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from django.core.asgi import get_asgi_application

import school.message_test.routing

application = ProtocolTypeRouter({
    'http': get_asgi_application(),

    'websocket': AuthMiddlewareStack(
        URLRouter(
            school.message_test.routing.websocket_urlpatterns
        )
    ),
})

0

There are 0 best solutions below