AttributeError: module ‘collections’ has no attribute ‘MutableMapping’ error occurs when you are trying to access an attribute MutableMapping on the collections module that does not exist.
Here are three ways to fix the error.
- Using collections.abs module
- Downgrading the Python version to 3.9 version or less
- Optional: Upgrade Python packages to the latest versions
In Python 3.3 through 3.9, abstract base classes (ABCs) like MutableMapping were located in the collections module. Starting with Python 3.10 and later, the MutableMapping class can be accessed as “collections.abc.MutableMapping”.
Flow diagram
Reproduce the error
import collections
main_dict = collections.MutableMapping
print(main_dict)
Output
AttributeError: module 'collections' has no attribute 'MutableMapping'
How to fix the error?
Solution 1: Use collections.abc module
Use the built-in Mapping class from the collections.abc module.
import collections
main_dict = collections.abc.MutableMapping
print(main_dict)
Output
<class 'collections.abc.MutableMapping'>
For version 3.10 or above:
from collections.abc import MutableMapping
For version 3.9 or lower:
from collections import MutableMapping
One classic solution that works for all Python versions is to use a dynamic import statement with a try-except block as follows:
try:
# Import for Python 3.10 and newer
from collections.abc import MutableMapping
except ImportError:
# Import for Python 3.9 and older
from collections import MutableMapping
# Example of a custom dictionary class using MutableMapping
class CustomDict(MutableMapping):
def __init__(self, data=None):
self._storage = dict() if data is None else data
def __getitem__(self, key):
return self._storage[key]
def __setitem__(self, key, value):
self._storage[key] = value
def __delitem__(self, key):
del self._storage[key]
def __iter__(self):
return iter(self._storage)
def __len__(self):
return len(self._storage)
# Example usage of the CustomDict
my_dict = CustomDict({'a': 1, 'b': 2})
print(my_dict['a'])
my_dict['c'] = 3
print(len(my_dict))
Output
1
3
Solution 2: Downgrading the Python version to 3.9 version or less
We will downgrade our Python version to 3.9 or any compatible lower version. All you need to install the lower version successfully.
Solution 3(optional): Upgrade Python packages to the latest versions
Based on your configurations, choose one of the below commands:
pip install --upgrade pip wheel setuptools requests
# For pip3:
pip3 install --upgrade pip wheel setuptools requests
# If pip is not in PATH:
python -m pip install --upgrade pip wheel setuptools requests
# python3:
python3 -m pip install --upgrade pip wheel setuptools requests
Related posts
AttributeError: str object has no attribute ‘decode’
AttributeError: ‘nonetype’ object has no attribute ‘shape’
Shiva
Thank you, this helped me indirectly …
In my case the problem package was tornado as seen below .. once I upgraded it, my Jupyter lab started working again. My point here is, the package that is causing this error needs to be investigated closely .. so Solution 3 might not work for all users at all times. Just sharing that.
ils.py”, line 30, in
from tornado.httpclient import AsyncHTTPClient, HTTPClient, HTTPRequest, HTTPResponse
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tornado/httpclient.py”, line 49, in
from tornado import httputil, stack_context
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tornado/httputil.py”, line 106, in
class HTTPHeaders(collections.MutableMapping):
^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module ‘collections’ has no attribute ‘MutableMapping’
➜ ~
Krunal
I am glad that this article helped you find the intended solution!