Last updated: Apr 9, 2024
Reading timeยท10 min
The Python "AttributeError: module 'collections' has no attribute 'MutableMapping'" occurs for multiple reasons:
MutableMapping
class from the collections
module in
Python versions 3.10+.MutableMapping
class from the
collections
module using Python versions 3.10+.There was a change in Python 3.10 and the MutableMapping
class has been moved
to the
collections.abc
module.
If you use Python version 3.10+, change your imports from the following.
import collections # ๐๏ธ Old import for versions older than Python3.10 # โ๏ธ AttributeError: module 'collections' has no attribute 'MutableMapping' print(collections.MutableMapping)
To import from the collections.abc
module.
import collections.abc #๐๏ธ New import for versions Python3.10+ # โ <class 'collections.abc.MutableMapping'> print(collections.abc.MutableMapping)
It's way more readable to import the MutableMapping
class directly from
collections.abc
.
from collections.abc import MutableMapping # ๐๏ธ <class 'collections.abc.MutableMapping'> print(MutableMapping)
Your error message will contain the file and line where the error is raised.
For example, the screenshot above shows that the error occurred in a main.py
file on line 3.
If your code needs to run in versions before and after Python 3.10, use a
try/except
statement.
try: # ๐๏ธ Using python 3.10+ from collections.abc import MutableMapping except ImportError: # ๐๏ธ Using python 3.10- from collections import MutableMapping # ๐๏ธ <class 'collections.abc.MutableMapping'> print(MutableMapping)
The try
statement tries to import the MutableMapping
class from the
collections.abc
module and if an ImportError
is raised, we know we are
running a version older than 3.10, so we import the class from the collections
module.
You can view all of the classes that are available in the collections.abc
module in
this section
of the docs.
If you got the error when pip
installing a third-party module, try upgrading
the module's version.
pip install requests --upgrade pip3 install requests --upgrade python3 -m pip install requests --upgrade
requests
with the name of the package you are trying to install.If that didn't help, try running the pip
install command with the --pre
option.
The --pre
option makes it so pip
includes pre-release and development
versions of the package. By default, pip
only finds stable versions.
pip install requests --pre pip3 install requests --pre python -m pip install requests --pre python3 -m pip install requests --pre
Make sure to replace requests
with the name of the actual package you are
trying to install.
--pre
if you need to get access to a feature that is not yet available in the stable version.This helps sometimes because there might be a prerelease version where the
import statement has been updated to
from collections.abc import MutableMapping
which is the correct import in
Python 3.10+.
collections
moduleAlternatively, 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.MutableMapping = collections.abc.MutableMapping collections.Mapping = collections.abc.Mapping collections.MutableSequence = collections.abc.MutableSequence collections.Sequence = collections.abc.Sequence collections.Iterable = collections.abc.Iterable collections.Iterator = collections.abc.Iterator collections.MutableSet = collections.abc.MutableSet collections.Callable = collections.abc.Callable # ๐๏ธ import the problematic module below # import problematic_module
You only have to add the attributes for the classes the module imports.
Make sure to import the module that causes the issue after you have added the necessary attributes.
You can check your Python version with the python --version
command.
python --version python3 --version
You can download a specific version (e.g. 3.9) from the official python.org website.
Different versions are available in the "Looking for a specific release" table.
Make sure to tick the following options if you get prompted:
To solve the "AttributeError: module collections has no attribute MutableMapping" error:
MutableMapping
class from collections.abc
, as a change was
made in Python 3.10.The Python "AttributeError: module 'collections' has no attribute 'Callable'" occurs for multiple reasons:
Callable
class from the collections
module in Python
versions 3.10+.Callable
class from the collections
module using Python versions 3.10+.There was a change in Python 3.10 and the Callable
class has been moved to the
collections.abc
module.
If you use Python version 3.10+, change your imports from the following.
import collections # ๐๏ธ Old import for versions older than Python3.10 # โ๏ธ AttributeError: module 'collections' has no attribute 'Callable' print(collections.Callable)
To import from the collections.abc
module.
import collections.abc #๐๏ธ New import for versions Python3.10+ # โ <class 'collections.abc.Callable'> print(collections.abc.Callable)
It's way more readable to import the Callable
class directly from
collections.abc
.
from collections.abc import Callable # ๐๏ธ <class 'collections.abc.Callable'> print(Callable)
Your error message will contain the file and line where the error is raised.
For example, the screenshot above shows that the error occurred in a main.py
file on line 3.
try/except
statement.try: # ๐๏ธ Using python 3.10+ from collections.abc import Callable except ImportError: # ๐๏ธ Using python 3.10- from collections import Callable # ๐๏ธ <class 'collections.abc.Callable'> print(Callable)
The try
statement tries to import the Callable
class from the
collections.abc
module and if an ImportError
is raised, we know we are
running a version older than 3.10, so we import the class from the collections
module.
If you got the error when pip
installing a third-party module, try upgrading
the module's version.
pip install requests --upgrade pip3 install requests --upgrade python3 -m pip install requests --upgrade
requests
with the name of the package you are trying to install.If that didn't help, try running the pip
install command with the --pre
option.
The --pre
option makes it so pip
includes pre-release and development
versions of the package. By default pip
only finds stable versions.
pip install requests --pre pip3 install requests --pre python -m pip install requests --pre python3 -m pip install requests --pre
Make sure to replace requests
with the name of the actual package you are
trying to install.
--pre
if you need to get access to a feature that is not yet available in the stable version.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.Callable = collections.abc.Callable collections.Mapping = collections.abc.Mapping collections.MutableMapping = collections.abc.MutableMapping collections.Iterable = collections.abc.Iterable collections.MutableSet = collections.abc.MutableSet # ๐๏ธ import the problematic module below # import problematic_module
You only have to add the attributes for the classes the module imports.
Make sure to import the module that causes the issue after you have added the necessary attributes.
To solve the "AttributeError: module collections has no attribute Callable" error:
Callable
class from collections.abc
, as a change was made in
Python 3.10.The Python "AttributeError: module 'collections' has no attribute 'Mapping'" occurs for multiple reasons:
Mapping
class from the collections
module in Python
versions 3.10+.Mapping
class from the collections
module using Python versions 3.10+.There was a change in Python 3.10 and the Mapping
class has been moved to the
collections.abc
module.
If you use Python version 3.10+, change your imports from the following.
import collections # ๐๏ธ Old import for versions older than Python3.10 # โ๏ธ AttributeError: module 'collections' has no attribute 'Mapping' print(collections.Mapping)
To import from the collections.abc
module.
import collections.abc #๐๏ธ New import for versions Python3.10+ # โ <class 'collections.abc.Mapping'> print(collections.abc.Mapping)
It's way more readable to import the Mapping
class directly from
collections.abc
.
from collections.abc import Mapping # ๐๏ธ <class 'collections.abc.Mapping'> print(Mapping)
Your error message will contain the file and line where the error is raised.
For example, the screenshot above shows that the error occurred in a main.py
file on line 3.
try/except
statement.try: # ๐๏ธ Using python 3.10+ from collections.abc import Mapping except ImportError: # ๐๏ธ Using python 3.10- from collections import Mapping # ๐๏ธ <class 'collections.abc.Mapping'> print(Mapping)
The try
statement tries to import the Mapping
class from the
collections.abc
module and if an ImportError
is raised, we know we are
running a version older than 3.10, so we import the class from the collections
module.
You can view all of the classes that are available in the collections.abc
module in
this section
of the docs.
If you got the error when pip
installing a third-party module, try upgrading
the module's version.
pip install requests --upgrade pip3 install requests --upgrade python3 -m pip install requests --upgrade
requests
with the name of the package you are trying to install.If that didn't help, try running the pip
install command with the --pre
option.
The --pre
option makes it so pip
includes pre-release and development
versions of the package. By default pip
only finds stable versions.
pip install requests --pre pip3 install requests --pre python -m pip install requests --pre python3 -m pip install requests --pre
Make sure to replace requests
with the name of the actual package you are
trying to install.
--pre
if you need to get access to a feature that is not yet available in the stable version.This helps sometimes because there might be a prerelease version where the
import statement has been updated to from collections.abc import Mapping
which
is the correct import in Python 3.10+.
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.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
You only have to add the attributes for the classes the module imports.
Make sure to import the module that causes the issue after you have added the necessary attributes.
To solve the "AttributeError: module collections has no attribute Mapping" error:
Mapping
class from collections.abc
, as a change was made in
Python 3.10.The Python "AttributeError: module 'collections' has no attribute 'Iterable'" occurs for multiple reasons:
Iterable
class from the collections
module in Python
versions 3.10+.Iterable
class from the collections
module using Python versions 3.10+.There was a change in Python 3.10 and the Iterable
class has been moved to the
collections.abc
module.
If you use Python version 3.10+, change your imports from the following.
import collections # ๐๏ธ Old import for versions older than Python3.10 # โ๏ธ AttributeError: module 'collections' has no attribute 'Iterable' print(collections.Iterable)
To import from the collections.abc
module.
import collections.abc #๐๏ธ New import for versions Python3.10+ # โ <class 'collections.abc.Iterable'> print(collections.abc.Iterable)
It's way more readable to import the Iterable
class directly from
collections.abc
.
from collections.abc import Iterable # ๐๏ธ <class 'collections.abc.Iterable'> print(Iterable)
Your error message will contain the file and line where the error is raised.
For example, the screenshot above shows that the error occurred in a main.py
file on line 3.
try/except
statement.try: # ๐๏ธ Using python 3.10+ from collections.abc import Iterable except ImportError: # ๐๏ธ Using python 3.10- from collections import Iterable # ๐๏ธ <class 'collections.abc.Iterable'> print(Iterable)
The try
statement tries to import the Iterable
class from the
collections.abc
module and if an ImportError
is raised, we know we are
running a version older than 3.10, so we import the class from the collections
module.
You can view all of the classes that are available in the collections.abc
module in
this section
of the docs.
If you got the error when pip
installing a third-party module, try upgrading
the module's version.
pip install requests --upgrade pip3 install requests --upgrade python3 -m pip install requests --upgrade
requests
with the name of the package you are trying to install.If that didn't help, try running the pip
install command with the --pre
option.
The --pre
option makes it so pip
includes pre-release and development
versions of the package. By default pip
only finds stable versions.
pip install requests --pre pip3 install requests --pre python -m pip install requests --pre python3 -m pip install requests --pre
Make sure to replace requests
with the name of the actual package you are
trying to install.
--pre
if you need to get access to a feature that is not yet available in the stable version.This helps sometimes because there might be a prerelease version where the
import statement has been updated to from collections.abc import Iterable
which is the correct import in Python 3.10+.
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.Iterable = collections.abc.Iterable collections.Mapping = collections.abc.Mapping collections.MutableMapping = collections.abc.MutableMapping collections.MutableSet = collections.abc.MutableSet collections.Callable = collections.abc.Callable # ๐๏ธ import the problematic module below # import problematic_module
You only have to add the attributes for the classes the module imports.
Make sure to import the module that causes the issue after you have added the necessary attributes.
To solve the "AttributeError: module collections has no attribute Iterable" error:
Iterable
class from collections.abc
, as a change was made in
Python 3.10.