Last updated: Apr 9, 2024
Reading time·14 min
Make sure to click on the correct subheading depending on your error message.
The Python "ImportError: cannot import name Mapping from 'collections'" 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+.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.
If you use Python version 3.10+, change your imports from the following.
# ⛔️ Old import for versions older than Python3.10 from collections import Mapping print(Mapping)
To import from the collections.abc
module.
# ✅ New import for versions Python3.10+ 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 1.
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 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+.
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.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.
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 Python "ImportError: cannot import name 'Mapping' from 'collections'" error:
Mapping
class from collections.abc
, as a change was made in
Python 3.10.The Python "ImportError: cannot import name 'MutableMapping' from 'collections'" 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+.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.
If you use Python version 3.10+, change your imports from the following.
# ⛔️ Old import for versions older than Python3.10 from collections import MutableMapping print(MutableMapping)
To import from the collections.abc
module.
# ✅ 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.
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.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.
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 Python "ImportError: cannot import name 'MutableMapping' from 'collections'" error:
MutableMapping
class from collections.abc
, as a change was
made in Python 3.10.The Python "ImportError: cannot import name 'Sequence' from 'collections'" occurs for multiple reasons:
Sequence
class from the collections
module in Python
versions 3.10+.Sequence
class from the collections
module using Python versions 3.10+.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.
If you use Python version 3.10+, change your imports from the following.
# ⛔️ Old import for versions older than Python3.10 from collections import Sequence print(Sequence)
To import from the collections.abc
module.
# ✅ 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.
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)
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.
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 Sequence
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.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.
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 Python "ImportError: cannot import name 'Sequence' from 'collections'" error:
Sequence
class from collections.abc
, as a change was made in
Python 3.10.The Python "ImportError: cannot import name 'Iterable' from 'collections'" 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+.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.
If you use Python version 3.10+, change your imports from the following.
# ⛔️ Old import for versions older than Python3.10 from collections import Iterable print(Iterable)
To import from the collections.abc
module.
# ✅ 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.
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+.
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.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.
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 Python "ImportError: cannot import name 'Iterable' from 'collections'" error:
Iterable
class from collections.abc
, as a change was made in
Python 3.10.The Python "ImportError: cannot import name Callable from 'collections'" 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+.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.
If you use Python version 3.10+, change your imports from the following.
# ⛔️ Old import for versions older than Python3.10 from collections import Callable print(Callable)
To import from the collections.abc
module.
# ✅ 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.
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.
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 Callable
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.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.
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 Python "ImportError: cannot import name 'Callable' from 'collections'" error:
Callable
class from collections.abc
, as a change was made in
Python 3.10.