5

I'm running this simple flask app in vs code. Dockerized it and loaded to ECS. The task is exiting with the following error:

ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/local/lib/python3.10/collections/init.py)

The base image I'm using is python3.10.

Following is the dockerfile code:

FROM python:3.10
WORKDIR /usr/src/app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000

Why am I getting this error?

0

4 Answers 4

6

Trying the following in python 3.9:

>>> from collections import MutableMapping
<stdin>: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

So I'm guessing your problem is you're using python 3.10 where you need to do:

>>> from collections.abc import MutableMapping
4
  • in this app, i'm not explicitly importing this module...following is the code: from flask import Flask, render_template import os import random app = Flask(name) # list of cat images images = [ "firebasestorage.googleapis.com/v0/b/…", "firebasestorage.googleapis.com/v0/b/…", Nov 18, 2021 at 5:41
  • how do I make the interpreter look for collections.abc rather than collections? Nov 18, 2021 at 5:43
  • @user13121562 this probably means Flask or something else is importing MutableMapping in this wrong way - consider trying to use an earlier (<=3.9) version of python3.
    – ssp
    Nov 18, 2021 at 6:42
  • @ssp..its working with 3.9! thanks! Nov 18, 2021 at 9:33
5

if you update the flask package to >=2.0 it will import it correctly also in python 3.10

3

Looks like you're doing the same docker tutorial that I am.

@geza.denes is right, we need flask 2.0 or greater.

you can update this in flask-app/requirements.txt

Rebuild your docker image and run it again, should work!

0

For Python 3.10.6

ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/local/lib/python3.10/collections/init.py)

For me Modules Mapping, MutableMapping and Sequence were missing in the /usr/local/lib/python3.10/collections/init.py adding them solved the problem.

from _collections_abc import Mapping
from _collections_abc import MutableMapping
from _collections_abc import Sequence

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.