Importerror cannot import name ‘mutablemapping’ from ‘collections’

One such error that can puzzle Python developers is the “ImportError: Cannot import name ‘MutableMapping’ from ‘collections‘” error.

This error message might seem daunting at first, but yeah it can fix!

In this article, we’ll delve into the depths of this ImportError, understand its causes, and explore effective solutions to resolve it.

What is importerror cannot import name ‘mutablemapping’ from ‘collections’?

The Python “ImportError: cannot import name ‘MutableMapping’ from ‘collections‘” occurs when we try to import the MutableMapping class from the collections module in Python versions 3.10+.

Additionally, it occurs when you install library from collections using 3.10 version of python.

Besides, Python 3.10 has been moved MutableMapping to collections.abc.

Fix – cannot import name ‘mutablemapping’ from ‘collections’

Here are the solutions to fix the error importerror cannot import name ‘mutablemapping’ from ‘collections’.

Update Import Statement

When you are using Python version 3.10+, replace your imports from the following statement:

# 📌 Old import for versions older than Python3.10
from collections import Sequence

print(Sequence)

To this import statement from the collections.abc module.

# ✅ New import for versions Python3.10+
from collections.abc import Sequence

# 📌 <class 'collections.abc.Sequence'>
print(Sequence)

Run code in Mulitple python version

Meanwhile, when you need to run the code prior to Python version 3.10, try to use try/except statement.

try:
    #📌 using Python 3.10+
    from collections.abc import Sequence
except ImportError:
    #📌 using Python 3.10-
    from collections import Sequence

#📌 <class 'collections.abc.Sequence'>
print(Sequence)

Technically, try statement tries to import sequence class from the collections.abc module however if an error is raised,

Absolutely the Python version is older than 3.10. Hence, we should import the class from the collections module.

Upgrade packages

Meanwhile, when installing pip third-party module got an error, you’ll need to upgrade to the latest version.

Here is how you can do it:

pip install <package name> --upgrade

pip3 install <package name> --upgrade

python3 -m pip install <package name> --upgrade

Take note to replace with the name of the package you are trying to install.

Add attributes to collections Module

Alternatively, you can add attributes to the collections module and point the attributes to the classes in collections.abc.

import collections.abc

#📌 add attributes to `collections` module
# before you import the package that causes the issue
collections.Sequence = collections.abc.Sequence
collections.Mapping = collections.abc.Mapping
collections.MutableMapping = collections.abc.MutableMapping
collections.Iterable = collections.abc.Iterable
collections.MutableSet = collections.abc.MutableSet
collections.Callable = collections.abc.Callable

#📌 import the problematic module below
# import problematic_module

Once you have added the required attributes, ensure that you import the module that is causing the issue.

Besides, here are other fixed errors, which might help you when encountering them.

Conclusion

In conclusion, importerror cannot import name ‘mutablemapping‘ from ‘collections’ typically occurs when trying to import MutableMapping from the collections module in Python 3.10+.

This is because the MutableMapping class has been deprecated since Python 3.3 and has been officially removed since Python 3.91.

To fix the error, instead of using from collections import MutableMapping, you should use from collections.abc import MutableMapping.

I think that’s all for this error. I hoped this article has helped you fix the issue.

Until next time! 😊

Leave a Comment