繁体   English   中英

未命名模块<app_name>

[英]No module named <app_name>

因此,我在一个单独的项目中使用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不能识别我的模块的错误,但是在其他地方却可以识别,所以为什么不在这里

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

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

后来我在website / 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

因此,如您所见,我在website / myWebsite目录中确实有__init__.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),
]

网站/网页/ settings.py

INSTALLED_APPS = [
    ~~ SNIP ~~
    'channels',
    'myWebsite',
    'django_countries',
    'social_django',
]
由于两者均无济于事,因此恢复为原始代码

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

除非我对这些事情的理解是错误的-完全有可能-否则会将website变成其自己的模块。 那样会将您在website/website/routing.py使用的绝对路径更改为website.myWebsite.routing 呸。

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

utils.py文件中有错误

当我在朋友的推荐下使用shell时,发现了此错误。

当我from myWebsite.routing import websocket_routing, custom_routing执行此操作时,它弹出from myWebsite.routing import websocket_routing, custom_routing

只是执行import myWebsite没有错误

这是屏幕截图:

https://ibb.co/m8zQ85

https://ibb.co/eAY7MQ

暂无
暂无

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

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