calen
calen

Reputation: 31

Daphne ModuleNotFoundError: No module named 'app_name'

When I run daphne -b 0.0.0.0 -p 8000 --access-log=daphne.log config.asgi:application I get Daphne ModuleNotFoundError: No module named 'app_name'

But when I run python3 manage.py runserver it works normally? When I remove app_1 from INSTALLED_APPS it will show me ModuleNotFoundError: No module named 'app_2'

This is my folder structure:

project_name
│   __init__.py    
│   manage.py    
│ 
└───config
│   │   __init__.py
│   │   asgi.py
│   │   celery.py
│   │   urls.py
│   │   wsgi.py
│   │
│   └───settings
│       │
│       │   __init__.py
│       │   base.py
│       │   dev.py
│       │   prod.py
│       
│
└───project_name
│   │   __init__.py
│   │
│   └───app_1
│   └───app_2
│   └───app_3
│   
└───media
│ 
└───static

asgi.py:

from django.core.asgi import get_asgi_application


django_asgi_app = get_asgi_application()


from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter

from chat import routing


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev')

application = ProtocolTypeRouter({
    'http': django_asgi_app,
    'websocket': AuthMiddlewareStack(
        URLRouter(
            routing.websocket_urlpatterns
        )
    ),
})

Upvotes: 1

Views: 3734

Answers (3)

aoulaa
aoulaa

Reputation: 84

import os

import django
django.setup()

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

from notification.routing import websocket_urlpatterns

from notification.utils.websocket_auth import WebSocketAuthMiddleware

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

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

In my case django setup fixed the problem.

just put django setup in asgi file.

Upvotes: 0

calen
calen

Reputation: 31

I had to append the folder where all my apps lie to the PYTHONPATH environment variable like so: PYTHONPATH="${PYTHONPATH}:/project_name/project_name"

Upvotes: 1

Large hare
Large hare

Reputation: 1

just like this. add a environment path to asgi.py

    from channels.auth import AuthMiddlewareStack
    import sys
    sys.path.append('your-project-abspath')
    import chat.routing
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'env.settings')

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

Upvotes: 0

Related Questions