• 카테고리

    질문 & 답변
  • 세부 분야

    웹 개발

  • 해결 여부

    해결됨

run_test_hello_channel.py 실행 no module named 'channel_redis' 오류 발생

23.03.09 15:53 작성 23.03.09 15:56 수정 조회수 409

0

 m1 mac에서 강의를 듣고있습니다.

env에서 channel_layer_redis 값을 파싱하는 것까지는 잘 따라왔는데

run_test_hello_channel.py 에서 해당 오류가 발생하네요

강사님 github에 있는 코드 그대로 복사해서 붙였는데도 오류가 나서 어디서부터 해결해야 할 지 모르겠습니다!도와주세요 ㅜㅜ

import asyncio

import os


import djangofrom channels.layers import get_channel_layer
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"django.setup()

async def main(): channel_layer = get_channel_layer()
message_dict = {'content': 'world'}
await channel_layer.send('hello', message_dict) response_dict = await channel_layer.receive('hello') is_equal = message_dict == response_dict print("송신/수신 데이터가 같습니까?", is_equal)

asyncio.run(main())

 

터미널 결과는 다음과 같습니다.

 

 

(webchat) tk_mac@itaegyeong-ui-MacBookAir webchat % python run_test_hello_channel.py

Traceback (most recent call last):

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/site-packages/channels/layers.py", line 65, in makebackend

    backend_class = import_string(self.configs[name]["BACKEND"])

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/site-packages/django/utils/module_loading.py", line 30, in import_string

    return cached_import(module_path, class_name)

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/site-packages/django/utils/module_loading.py", line 15, in cached_import

    module = import_module(module_path)

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/importlib/__init__.py", line 126, in import_module

    return bootstrap.gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 1050, in gcdimport

  File "<frozen importlib._bootstrap>", line 1027, in findand_load

  File "<frozen importlib._bootstrap>", line 992, in findand_load_unlocked

  File "<frozen importlib._bootstrap>", line 241, in callwith_frames_removed

  File "<frozen importlib._bootstrap>", line 1050, in gcdimport

  File "<frozen importlib._bootstrap>", line 1027, in findand_load

  File "<frozen importlib._bootstrap>", line 1004, in findand_load_unlocked

ModuleNotFoundError: No module named 'channel_redis'

 

During handling of the above exception, another exception occurred:

 

Traceback (most recent call last):

  File "/Users/tk_mac/github/webchat/run_test_hello_channel.py", line 22, in <module>

    asyncio.run(main())

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/asyncio/runners.py", line 44, in run

    return loop.run_until_complete(main)

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete

    return future.result()

  File "/Users/tk_mac/github/webchat/run_test_hello_channel.py", line 12, in main

    channel_layer = get_channel_layer()

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/site-packages/channels/layers.py", line 357, in get_channel_layer

    return channel_layers[alias]

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/site-packages/channels/layers.py", line 78, in getitem

    self.backends[key] = self.make_backend(key)

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/site-packages/channels/layers.py", line 44, in make_backend

    return self._make_backend(name, config)

  File "/Users/tk_mac/opt/anaconda3/envs/webchat/lib/python3.10/site-packages/channels/layers.py", line 69, in makebackend

    raise InvalidChannelLayerError(

channels.exceptions.InvalidChannelLayerError: Cannot import BACKEND 'channel_redis.core.RedisChannelLayer' specified for default

 

 

답변 2

·

답변을 작성해보세요.

1

안녕하세요.

channels-redis 라이브러리에 대한 ModuleNotFoundError가 발생하고 계시네요. 이 문제는 현재 python 명령으로 참조하고 있는 가상환경에 channels-redis 라이브러리가 설치되어있지 않아서 발생하는 이슈입니다.

파이썬은 라이브러리를 전역공간에 설치할 수도 있고, 각 가상환경마다 설치하실 수도 있는 데,
라이브러리를 전역공간에 설치하시면 가상환경에서는 없으니 ModuleNotFoundError가 발생하실 수 있습니다.

현재의 python 명령이 바라보는 곳에 정확히 라이브러리를 설치하고, 이를 인지하실 수 있으셔야, 향후에도 이런 오류를 만나실 때 손쉽게 해결하실 수 있습니다.

현재 오류를 만나신 터미널에서

python -m pip list 명령으로 설치된 라이브러리 목록을 확인하실 수 있는 데, channels-redis가 있는 지 확인해보시구요.

이어서 python -m pip install 팩키지명 명령을 통해 channels-redis 라이브러리를 설치하시고, 다시 python run_test_hello_channel.py 명령을 수행해보시겠어요?

차근차근 확인해보시고, 댓글 부탁드립니다.

화이팅입니다. ;-)

0

이태경님의 프로필

이태경

질문자

2023.03.09

빠르게 답변해주셔서 감사합니다!

라이브러리를 검색해보니 설치가 되어있는 것으로 나옵니다 ㅜㅜ

(webchat) tk_mac@itaegyeong-ui-MacBookAir webchat % python -m pip list     

Package            Version

------------------ ---------

asgiref            3.6.0

async-timeout      4.0.2

attrs              22.2.0

autobahn           23.1.2

Automat            22.10.0

beautifulsoup4     4.11.2

certifi            2022.12.7

cffi               1.15.1

channels           4.0.0

channels-redis     4.0.0

charset-normalizer 3.1.0

constantly         15.1.0

cryptography       39.0.2

daphne             4.0.0

Django             4.1.7

django-bootstrap5  22.2

django-environ     0.10.0

exceptiongroup     1.1.0

hyperlink          21.0.0

idna               3.4

incremental        22.10.0

iniconfig          2.0.0

msgpack            1.0.4

packaging          23.0

pip                22.3.1

pluggy             1.0.0

pyasn1             0.4.8

pyasn1-modules     0.2.8

pycparser          2.21

pyOpenSSL          23.0.0

pytest             7.2.2

pytest-asyncio     0.20.3

pytest-django      4.5.2

pytest-testdox     3.0.1

redis              4.5.1

requests           2.28.2

service-identity   21.1.0

setuptools         65.6.3

six                1.16.0

soupsieve          2.4

sqlparse           0.4.3

tomli              2.0.1

Twisted            22.10.0

txaio              23.1.1

typing_extensions  4.5.0

urllib3            1.26.14

wheel              0.38.4

zope.interface     5.5.2

오타같습니다.

channel_redis 로 쓰신 듯 한데요. channels_redis 입니다. :-)

이태경님의 프로필

이태경

질문자

2023.03.09

settings.py에 channel layer 정의할때 오타가 있었네요! 감사합니다!