简体   繁体   English

未命名模块<app_name>

[英]No module named <app_name>

So, I made a chat app using django-channels in a separate project and now I am copying it into the main project. 因此,我在一个单独的项目中使用django-channels制作了一个聊天应用程序,现在将其复制到主项目中。

This is what is happening. 这就是正在发生的事情。 when I run ./manage.py runserver 当我运行./manage.py runserver

Unhandled exception in thread started by <function wrapper at 0x7f695c1cfde8>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/channels/management/commands/runserver.py", line 40, in inner_run
    self.channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]
  File "/usr/local/lib/python2.7/dist-packages/channels/asgi.py", line 53, in __getitem__
    self.backends[key] = self.make_backend(key)
  File "/usr/local/lib/python2.7/dist-packages/channels/asgi.py", line 48, in make_backend
    routing=routing,
  File "/usr/local/lib/python2.7/dist-packages/channels/asgi.py", line 80, in __init__
    self.router = Router(self.routing)
  File "/usr/local/lib/python2.7/dist-packages/channels/routing.py", line 25, in __init__
    self.root = Include(routing)
  File "/usr/local/lib/python2.7/dist-packages/channels/routing.py", line 201, in __init__
    self.routing = Router.resolve_routing(routing)
  File "/usr/local/lib/python2.7/dist-packages/channels/routing.py", line 75, in resolve_routing
    raise ImproperlyConfigured("Cannot import channel routing %r: %s" % (routing, e))
django.core.exceptions.ImproperlyConfigured: Cannot import channel routing 'website.routing.channel_routing': No module named myWebsite

I know this is some error with django not recognising my module , but in other places it's recognising so why not here 我知道这是django不能识别我的模块的错误,但是在其他地方却可以识别,所以为什么不在这里

The culprit code, the reason why I am stuck here for 2 days : 罪魁祸首代码,我被困在这里两天的原因:

website/website/routing.py 网站/网页/ routing.py

from channels import include
from myWebsite.routing import websocket_routing, custom_routing


channel_routing = [
    # Include sub-routing from an app.
    include(websocket_routing, path=r"^/chat/stream"),
    include(custom_routing),
]

website/myWebsite/routing.py 网站/ myWebsite / routing.py

from channels import route
from .consumers import ws_connect, ws_receive, ws_disconnect, chat_join, chat_leave, chat_send


websocket_routing = [
    route("websocket.connect", ws_connect),

    route("websocket.receive", ws_receive),

    route("websocket.disconnect", ws_disconnect),
]


custom_routing = [
    # Handling different chat commands (websocket.receive is decoded and put
    # onto this channel) - routed on the "command" attribute of the decoded
    # message.
    route("chat.receive", chat_join, command="^join$"),
    route("chat.receive", chat_leave, command="^leave$"),
    route("chat.receive", chat_send, command="^send$"),
]

Later I added this in website/myWebsite/__init.py__ : 后来我在website / myWebsite / __ init.py__中添加了它:

default_app_config='myWebsite.apps.MywebsiteConfig'

website/website/settings.py 网站/网页/ settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myWebsite',
    'django_countries',
    'social_django',
    'channels',
]

ROOT_URLCONF = 'website.urls'

The directory structure : 目录结构:

website
├── __init__.py
├── manage.py
├── myWebsite
│   ├── admin.py
│   ├── apps.py
│   ├── backends.py
│   ├── constants.py
│   ├── consumers.py
│   ├── exceptions.py
│   ├── forms.py
│   ├── __init__.py
│   ├── media
│   ├── migrations
│   │   ├── 0001_initial.py
~~ SNIP ~~
│   ├── models.py
│   ├── routing.py
│   ├── settings.py
│   ├── signals.py
│   ├── static
 ~~ SNIP ~~
│   ├── templates
 ~~ SNIP ~~
│   ├── tests.py
│   ├── urls.py
│   ├── utils.py
│   └── views.py
└── website
    ├── config.py
    ├── __init__.py
    ├── routing.py
    ├── settings.py
    ├── urls.py
    ├── views.py
    └── wsgi.py

So as you can see well above I do have the __init__.py in website/myWebsite directory. 因此,如您所见,我在website / myWebsite目录中确实有__init__.py

Any help would be greatly appreciated. 任何帮助将不胜感激。 It has stalled my work for the last 2 days as I have tried it all. 我已经尝试了所有这一切,这使我过去两天的工作停滞了。

Thanks 谢谢

Update As per comments 根据评论更新

New website/website/routing.py 新网站/网站/routing.py

from channels import include
import sys
from myWebsite.routing import websocket_routing, custom_routing


print(sys.path)
channel_routing = [
    include(websocket_routing, path=r"^/chat/stream"),
    include(custom_routing),
]

website/website/settings.py 网站/网页/ settings.py

INSTALLED_APPS = [
    ~~ SNIP ~~
    'channels',
    'myWebsite',
    'django_countries',
    'social_django',
]
Since neither of it help so reverting to the original code 由于两者均无济于事,因此恢复为原始代码

Why is there an __init__.py in the main directory? 为什么主目录中有一个__init__.py None of my Django projects do. 我的Django项目都没有。

Unless my understanding of these things is wrong —which is entirely possible— that would be turning website into its own module. 除非我对这些事情的理解是错误的-完全有可能-否则会将website变成其自己的模块。 That would change the absolute path you're using in website/website/routing.py to website.myWebsite.routing . 那样会将您在website/website/routing.py使用的绝对路径更改为website.myWebsite.routing Yuck. 呸。

You could update the absolute path in the import, but honestly, I'd just nuke the file and not turn the active directory into a module. 您可以更新导入中的绝对路径,但老实说,我只是核对文件,而不是将活动目录转换为模块。

There was an error in the utils.py file utils.py文件中有错误

I found this error when I used shell on my friend's recommendation. 当我在朋友的推荐下使用shell时,发现了此错误。

It popped up when I executed this from myWebsite.routing import websocket_routing, custom_routing 当我from myWebsite.routing import websocket_routing, custom_routing执行此操作时,它弹出from myWebsite.routing import websocket_routing, custom_routing

and there was no error while just executing import myWebsite 只是执行import myWebsite没有错误

Here are the screenshot : 这是屏幕截图:

https://ibb.co/m8zQ85 https://ibb.co/m8zQ85

https://ibb.co/eAY7MQ https://ibb.co/eAY7MQ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM