Daphne ModulenotFoundError:No模块名为' app_name'

发布于 2025-01-25 15:49:35 字数 1309 浏览 4 评论 0原文

当我运行Daphne -B 0.0.0.0 -p 8000 -Access -log = daphne.log config.asgi:应用程序 我获得Daphne ModulenotFoundError:no模块名为“ App_name”

但是当我运行Python3 Manage.py Runserver时,它正常工作吗? 当我从installed_apps中删除app_1时,它将向我显示modulenotfounderror:no模块名为'app_2'

这是我的文件夹结构:

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
        )
    ),
})

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
        )
    ),
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还在原地等你 2025-02-01 15:49:35

我不得不将所有应用都位于PythonPath环境变量的文件夹附加到这样的文件夹:
pythonpath =“ $ {pythonpath}:/project_name/project_name”

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

赴月观长安 2025-02-01 15:49:35

就像这样。 为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
            )
        ),
    }
)

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
            )
        ),
    }
)
梦言归人 2025-02-01 15:49:35
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)
        ),
    }
)

在我的情况下,django设置解决了问题。

只需将Django设置放在ASGI文件中即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文