简体   繁体   中英

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.

This is what is happening. when I run ./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

The culprit code, the reason why I am stuck here for 2 days :

website/website/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

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__ :

default_app_config='myWebsite.apps.MywebsiteConfig'

website/website/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.

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

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

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? None of my Django projects do.

Unless my understanding of these things is wrong —which is entirely possible— that would be turning website into its own module. That would change the absolute path you're using in website/website/routing.py to 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

I found this error when I used shell on my friend's recommendation.

It popped up when I executed this from myWebsite.routing import websocket_routing, custom_routing

and there was no error while just executing import myWebsite

Here are the screenshot :

https://ibb.co/m8zQ85

https://ibb.co/eAY7MQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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