ImportError: cannot import name Mapping from 'collections'

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
14 min

banner

# Table of Contents

  1. Cannot import name Mapping from 'collections'
  2. Cannot import name MutableMapping from 'collections'
  3. Cannot import name Sequence from 'collections'
  4. Cannot import name Iterable from 'collections'
  5. Cannot import name Callable from 'collections'

Make sure to click on the correct subheading depending on your error message.

# ImportError: cannot import name Mapping from 'collections'

The Python "ImportError: cannot import name Mapping from 'collections'" 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+.

importerror cannot import name mapping from collections

shell
from collections import Mapping ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py) [end of output]

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

# Update your import statements

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

main.py
# ⛔️ Old import for versions older than Python3.10 from collections import Mapping print(Mapping)

To import from the collections.abc module.

main.py
# ✅ New import for versions Python3.10+ from collections.abc import Mapping # 👇️ <class 'collections.abc.Mapping'> print(Mapping)

import from collections abc

Your error message will contain the file and line where the error is raised.

where error occurred

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

# Running your code in multiple Python versions

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)

universal imports that work in all python versions

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.

# Upgrade packages that cause the error

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

upgrade package that caused error

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

# Patch the error by adding attributes to 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.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.

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 Python "ImportError: cannot import name 'Mapping' from 'collections'" 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.

# Cannot import name MutableMapping from 'collections' - Python

The Python "ImportError: cannot import name 'MutableMapping' from 'collections'" 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+.

importerror cannot import name mutablemapping from collections

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

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

# Update your import statements

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

main.py
# ⛔️ Old import for versions older than Python3.10 from collections import MutableMapping print(MutableMapping)

To import from the collections.abc module.

main.py
# ✅ New import for versions Python3.10+ 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.

# Running your code in multiple Python versions

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)

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.

# Upgrade packages that cause the error

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

# Patch the error by adding attributes to 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.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.

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 Python "ImportError: cannot import name 'MutableMapping' from 'collections'" 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.

# ImportError: cannot import name Sequence from 'collections'

The Python "ImportError: cannot import name 'Sequence' from 'collections'" occurs for multiple reasons:

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

importerror cannot import name sequence from collections

shell
ImportError: cannot import name 'Sequence' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

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

# Update your import statements

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

main.py
# ⛔️ Old import for versions older than Python3.10 from collections import Sequence print(Sequence)

To import from the collections.abc module.

main.py
# ✅ New import for versions Python3.10+ from collections.abc import Sequence # 👇️ <class 'collections.abc.Sequence'> print(Sequence)

Your error message will contain the file and line where the error is raised.

# Running your code in multiple Python versions

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 Sequence except ImportError: # 👇️ Using python 3.10- from collections import Sequence # 👇️ <class 'collections.abc.Sequence'> print(Sequence)

The try statement tries to import the Sequence 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.

# Upgrade packages that cause the error

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 Sequence which is the correct import in Python 3.10+.

# Patch the error by adding attributes to 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.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

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 Python "ImportError: cannot import name 'Sequence' from 'collections'" error:

  1. Import the Sequence 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.

# ImportError: cannot import name Iterable from 'collections'

The Python "ImportError: cannot import name 'Iterable' from 'collections'" 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+.

importerror cannot import name iterable from collections

shell
ImportError: cannot import name 'Iterable' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

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

# Update your import statements

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

main.py
# ⛔️ Old import for versions older than Python3.10 from collections import Iterable print(Iterable)

To import from the collections.abc module.

main.py
# ✅ New import for versions Python3.10+ 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.

# Running your code in multiple Python versions

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.

# Upgrade packages that cause the error

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

# Patch the error by adding attributes to 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.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.

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 Python "ImportError: cannot import name 'Iterable' from 'collections'" 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.

# ImportError: cannot import name Callable from 'collections'

The Python "ImportError: cannot import name Callable from 'collections'" 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+.

importerror cannot import name callable from collections

shell
ImportError: cannot import name 'Callable' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

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

# Update your import statements

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

main.py
# ⛔️ Old import for versions older than Python3.10 from collections import Callable print(Callable)

To import from the collections.abc module.

main.py
# ✅ New import for versions Python3.10+ 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.

# Running your code in multiple Python versions

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.

You can view all of the classes that are available in the collections.abc module in this section of the docs.

# Upgrade packages that cause the error

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 Callable which is the correct import in Python 3.10+.

# Patch the error by adding attributes to 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.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.

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 Python "ImportError: cannot import name 'Callable' from 'collections'" 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.
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.