Cannot import ASGI_APPLICATION module while runserver using channels 2

删除回忆录丶 提交于 2019-12-22 04:01:08

问题


I have followed the channels tutorial but while running these error throw

Version of the packages is channels==2.1.2 Django==2.0.4

what I missed ? in settings.py

INSTALLED_APPS = [
   "channels"
    ....
]

ROOT_URLCONF = 'myapp.urls'
ASGI_APPLICATION = "myapp.routing.application"

added file mayapp/routing.py

from channels.routing import ProtocolTypeRouter 

application = ProtocolTypeRouter({
    # Empty for now (http->django views is added by default)
})

this is the error log

System check identified no issues (0 silenced).
August 01, 2018 - 13:11:42
Django version 2.0.4, using settings 'myapp.local_settings'
Starting ASGI/Channels version 2.1.2 development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f71ecfb6400>
Traceback (most recent call last):
  File "/home/vkchlt0192/myapp/lib/python3.5/site-packages/channels/routing.py", line 33, in get_default_application
    module = importlib.import_module(path)
  File "/home/vkchlt0192/myapp/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'myapp.routing'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/vkchlt0192/myapp/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/vkchlt0192/myapp/lib/python3.5/site-packages/channels/management/commands/runserver.py", line 80, in inner_run
    application=self.get_application(options),
  File "/home/vkchlt0192/myapp/lib/python3.5/site-packages/channels/management/commands/runserver.py", line 105, in get_application
    return StaticFilesWrapper(get_default_application())
  File "/home/vkchlt0192/myapp/lib/python3.5/site-packages/channels/routing.py", line 35, in get_default_application
    raise ImproperlyConfigured("Cannot import ASGI_APPLICATION module %r" % path)
django.core.exceptions.ImproperlyConfigured: Cannot import ASGI_APPLICATION module 'myapp.routing'

回答1:


You need to put your routing.py file inside mayapp/mayapp/routing.py instead of mayapp/routing.py




回答2:


Just change

ASGI_APPLICATION = mysite.routing.application

to

ASGI_APPLICATION = "routing.application"




回答3:


Check for any potential errors (maybe import error) in consumers.py. Also, try to put channels as the first item in INSTALLED_APPS in settings.py.

As stated in channels document:

The Channels development server will conflict with any other third-party apps that require an overloaded or replacement runserver command. An example of such a conflict is with whitenoise.runserver_nostatic from whitenoise. In order to solve such issues, try moving channels to the top of your INSTALLED_APPS or remove the offending app altogether.




回答4:


in my case there were unresolved packages in consumer.py check if you have any unresolved packages in your channels .py files




回答5:


I was also having the same issue i did everything to resolve it, creating another virtual environment and installing older version of Django but after 2 days of hardware i came to realize that my consumers.py file was missing with just a 's' in consumer's' and after that i also corrected in my routing.py file. May be this could be your problem also please do check all the file names first.




回答6:


In case anybody comes along this. Remember: ASGI_APPLICATION = "myapp.routing.application" should go at the bottom of settings.py to ensure nothing get snagged in production!

mysite/myapp/routing.py

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

application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AuthMiddlewareStack(
        URLRouter(
            myapp.routing.websocket_urlpatterns
        )
    ),
})

myapp/routing.py

from django.urls import path
from . import consumers

websocket_urlpatterns = [
    path('chatroompage', consumers.ChatConsumer),
]


来源:https://stackoverflow.com/questions/51634522/cannot-import-asgi-application-module-while-runserver-using-channels-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!