Home > OS >  django rest Error - AttributeError: module 'collections' has no attribute 'MutableMap
django rest Error - AttributeError: module 'collections' has no attribute 'MutableMap

Time:10-06

I'm build Django app, and it's work fine on my machine, but when I run inside docker container it's rest framework keep crashing, but when I comment any connection with rest framework it's work fine.

  • My machine: Kali Linux 2021.3
  • docker machine: Raspberry Pi 4 4gb
  • docker container image: python:rc-alpine3.14
  • python version on my machine: Python 3.9.7
  • python version on container: Python 3.10.0rc2

error output:

Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/test.py", line 55, in handle
    failures = test_runner.run_tests(test_labels)
  File "/usr/local/lib/python3.10/site-packages/django/test/runner.py", line 728, in run_tests
    self.run_checks(databases)
  File "/usr/local/lib/python3.10/site-packages/django/test/runner.py", line 665, in run_checks
    call_command('check', verbosity=self.verbosity, databases=databases)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 181, in call_command
    return command.execute(*args, **defaults)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/check.py", line 63, in handle
    self.check(
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 419, in check
    all_issues = checks.run_checks(
  File "/usr/local/lib/python3.10/site-packages/django/core/checks/registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 412, in check
    for pattern in self.url_patterns:
  File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 598, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/local/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 _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/app/project/urls.py", line 24, in <module>
    path("", include("apps.urls", namespace="apps")),
  File "/usr/local/lib/python3.10/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/local/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 _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/app/apps/urls.py", line 3, in <module>
    from . import api
  File "/app/apps/api.py", line 1, in <module>
    from .serializers import *
  File "/app/apps/serializers.py", line 1, in <module>
    from rest_framework import serializers
  File "/usr/local/lib/python3.10/site-packages/rest_framework/serializers.py", line 27, in <module>
    from rest_framework.compat import postgres_fields
  File "/usr/local/lib/python3.10/site-packages/rest_framework/compat.py", line 59, in <module>
    import requests
  File "/usr/local/lib/python3.10/site-packages/requests/__init__.py", line 63, in <module>
    from . import utils
  File "/usr/local/lib/python3.10/site-packages/requests/utils.py", line 27, in <module>
    from .cookies import RequestsCookieJar, cookiejar_from_dict
  File "/usr/local/lib/python3.10/site-packages/requests/cookies.py", line 172, in <module>
    class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
AttributeError: module 'collections' has no attribute 'MutableMapping'

Dockerfile

FROM python:rc-alpine3.14

COPY . /app 
WORKDIR /app

ENV UWSGI_PROFILE=core
ENV PYTHONUNBUFFERED=TRUE
RUN apk add --update --no-cache g   gcc libxslt-dev # add some nessery libs
RUN apk add python3-dev build-base linux-headers pcre-dev
RUN pip3 install -U pip # upgrade pip
RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add jpeg-dev zlib-dev libjpeg \
    && pip install Pillow \
    && apk del build-deps


RUN apk add --no-cache --virtual=build-dependencies wget ca-certificates && \
    wget "https://bootstrap.pypa.io/get-pip.py" -O /dev/stdout | python


RUN pip install uwsgi

RUN pip3 install -r requirements.txt # install all requirements
RUN python uploadstatics.py

EXPOSE 80
CMD ["gunicorn","project.wsgi:application", "-b 0.0.0.0:8060"]

NOTES:

  • I use gunicorn to run the app (the error show even I run't from manage.py)

CodePudding user response:

Either you can downgrade your python version. That should solve your problem if not, then,use collections.abc.Mapping instead of deprecated collections.Mapping.

Refer here: Link

CodePudding user response:

In your docker file you set the image as python:rc-alpine3.14 which uses python 3.10. Considering that 3.10 is very recently released, many of the packages are yet to upgrade and depending on the version constraints you have in your requirements.txt that might also cause incompatible versions. The problem here is Django Rest framework uses the requests package which is using collections.MutableMapping, if I write this in python 3.9 I get the following warning which says this will be removed in 3.10, hence you should downgrade for now to python 3.9:

Warning (from warnings module):
  File "<pyshell#1>", line 1
DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working

Another thing about writing python:rc-alpine3.14 as the image is just asking for your code to break! Specify a specific stable and released version rather than the release candidate rc:

FROM python:3.9.7-alpine3.14

...
  • Related