Module collections has no attribute 'MutableMapping' [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
10 min

banner

# Table of Contents

  1. Module collections has no attribute 'MutableMapping'
  2. Module collections has no attribute Callable
  3. Module collections has no attribute Mapping
  4. Module collections has no attribute Iterable

# Module collections has no attribute 'MutableMapping'

The Python "AttributeError: module 'collections' has no attribute 'MutableMapping'" occurs for multiple reasons:

  1. Trying to import the MutableMapping class from the collections module in Python versions 3.10+.
  2. Installing a module that imports the MutableMapping class from the collections module using Python versions 3.10+.

attributeerror module collections has no attribute mutablemapping

There was a change in Python 3.10 and the MutableMapping class has been moved to the collections.abc module.

# Update your import statements for Python 3.10+

If you use Python version 3.10+, change your imports from the following.

main.py
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.

main.py
import collections.abc #๐Ÿ‘‡๏ธ New import for versions Python3.10+ # โœ… <class 'collections.abc.MutableMapping'> print(collections.abc.MutableMapping)

import from collections abc module

It's way more readable to import the MutableMapping class directly from collections.abc.

main.py
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.

attributeerror module collections has no attribute mutablemapping

For example, the screenshot above shows that the error occurred in a main.py file on line 3.

# If your code needs to run before and after Python 3.10

If your code needs to run in versions before and after Python 3.10, use a try/except statement.

main.py
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)

using universal import that works in python2 and 3

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 installing a third-party module

If you got the error when pip installing a third-party module, try upgrading the module's version.

shell
pip install requests --upgrade pip3 install requests --upgrade python3 -m pip install requests --upgrade
Make sure to replace 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.

shell
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.

You would want to run the command with --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+.

# Patching the necessary attributes on the collections module

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

main.py
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.

Another way to solve the error is to revert to Python 3.9 as the change was introduced in Python 3.10.

You can check your Python version with the python --version command.

shell
python --version python3 --version

get python 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.

install specific python version

Make sure to tick the following options if you get prompted:

  • Install launcher for all users (recommended)
  • Add Python to PATH (this adds Python to your PATH environment variable)

To solve the "AttributeError: module collections has no attribute MutableMapping" error:

  1. Import the MutableMapping class from collections.abc, as a change was made in Python 3.10.
  2. Update the versions of any modules that have old import statements.
  3. Alternatively, revert to Python 3.9 if you are unable to make corrections.

# Table of Contents

  1. Module collections has no attribute Callable
  2. Module collections has no attribute Mapping
  3. Module collections has no attribute Iterable

# AttributeError: module collections has no attribute Callable

The Python "AttributeError: module 'collections' has no attribute 'Callable'" occurs for multiple reasons:

  1. Trying to import the Callable class from the collections module in Python versions 3.10+.
  2. Installing a module that imports the Callable class from the collections module using Python versions 3.10+.

attributeerror module collections has no attribute callable

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.

main.py
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.

main.py
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.

main.py
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.

attributeerror module collections has no attribute callable

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.
main.py
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.

shell
pip install requests --upgrade pip3 install requests --upgrade python3 -m pip install requests --upgrade
Make sure to replace 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.

shell
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.

You would want to run the command with --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.

main.py
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:

  1. Import the Callable class from collections.abc, as a change was made in Python 3.10.
  2. Update the versions of any modules that have old import statements.
  3. Alternatively, revert to Python 3.9 if you are unable to make corrections.

# Table of Contents

  1. Module collections has no attribute Mapping
  2. Module collections has no attribute Iterable

# AttributeError: module collections has no attribute Mapping

The Python "AttributeError: module 'collections' has no attribute 'Mapping'" occurs for multiple reasons:

  1. Trying to import the Mapping class from the collections module in Python versions 3.10+.
  2. Installing a module that imports the Mapping class from the collections module using Python versions 3.10+.

attributeerror module collections has no attribute mapping

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.

main.py
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.

main.py
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.

main.py
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.

attributeerror module collections has no attribute mapping

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.
main.py
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.

shell
pip install requests --upgrade pip3 install requests --upgrade python3 -m pip install requests --upgrade
Make sure to replace 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.

shell
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.

You would want to run the command with --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.

main.py
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:

  1. Import the Mapping class from collections.abc, as a change was made in Python 3.10.
  2. Update the versions of any modules that have old import statements.
  3. Alternatively, revert to Python 3.9 if you are unable to make corrections.

# AttributeError: module collections has no attribute Iterable

The Python "AttributeError: module 'collections' has no attribute 'Iterable'" occurs for multiple reasons:

  1. Trying to import the Iterable class from the collections module in Python versions 3.10+.
  2. Installing a module that imports the Iterable class from the collections module using Python versions 3.10+.

attributeerror module collections has no attribute iterable

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.

main.py
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.

main.py
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.

main.py
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.

attributeerror module collections has no attribute iterable

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.
main.py
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.

shell
pip install requests --upgrade pip3 install requests --upgrade python3 -m pip install requests --upgrade
Make sure to replace 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.

shell
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.

You would want to run the command with --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.

main.py
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:

  1. Import the Iterable class from collections.abc, as a change was made in Python 3.10.
  2. Update the versions of any modules that have old import statements.
  3. Alternatively, revert to Python 3.9 if you are unable to make corrections.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev