没有名为<app_name>的模块

时间:2017-04-09 06:15:02

标签: python django django-channels

所以,我在一个单独的项目中使用django-channels制作了一个聊天应用程序,现在我将它复制到主项目中。

这就是发生的事情。当我运行./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

我知道这是一个错误,django没有识别我的模块,但在其他地方,它认识到为什么不在这里

罪魁祸首代码,我被困在这里2天的原因:

网站/网页/ 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),
]

网站/ 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$"),
]

后来我在网站/ myWebsite / __ init.py__中添加了这个:

default_app_config='myWebsite.apps.MywebsiteConfig'

网站/网页/ 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'

目录结构:

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

因此,您可以在上面看到我在网站/ myWebsite 目录中有__init__.py

非常感谢任何帮助。在我试过这一切的过程中,它在过去的两天里一直停滞不前。

感谢

根据评论更新

新网站/ website / 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),
]

网站/网页/ settings.py

INSTALLED_APPS = [
    ~~ SNIP ~~
    'channels',
    'myWebsite',
    'django_countries',
    'social_django',
]
因为它们都没有帮助如此恢复到原始代码

2 个答案:

答案 0 :(得分:0)

为什么主目录中有__init__.py?我的Django项目都没有。

除非我对这些事情的理解是错误的 - 这是完全可能的 - 这会将website转变为自己的模块。这会改变您在website/website/routing.pywebsite.myWebsite.routing中使用的绝对路径。呸。

您可以更新导入中的绝对路径,但老实说,我只是将文件编入核心,而不是将活动目录转换为模块。

答案 1 :(得分:0)

utils.py文件中存在错误

当我在朋友的建议中使用shell时,我发现了这个错误。

执行此from myWebsite.routing import websocket_routing, custom_routing

时弹出

并且在执行import myWebsite

时没有错误

以下是截图:

https://ibb.co/m8zQ85

https://ibb.co/eAY7MQ