Importerror: cannot import name ‘mapping’ from ‘collections’

Importerror_ cannot import name 'mapping' from 'collections'

importerror: cannot import name ‘mapping’ from ‘collections’ error occurs while importing Mapping class from collections module in Higher version of python ( 3.10 + ). Actually Mapping class is now moved in collections.abc class. Actually in most of the case we do not know the run time version of python because of multiple python version installation in the server. Since the code compatibility comes along with interpreter version hence in this article we will explore the best ways to fix up this error.

Importerror: cannot import name ‘mapping’ from ‘collections’ ( Solution ) –

There are multiple ways to fix this error. Lets explore one by one. But before that lets replicate this issue.

Importerror cannot import name 'mapping' from 'collections'
Importerror cannot import name ‘mapping’ from ‘collections’

Since this importerror, hence most of the solutions are on compatibility only.

Solution 1 : Changing import statement –

this solution will work if the current python version is greater than 3.10+ , since the underline packaging is different in higher version then use the below import statement.

from collections.abc import Mapping

if you are maintaining the lower version, use the below code.

from collections import Mapping

Solution 2 : Adjusting the python version –

If you can not change the code but the flexibility to configure the environment variables. Then you can downgrade the python version of you are not adding ‘abc’ sub-package then downgrade the python to 3.10 version. If you have added in the code then upgrade the python version.

Here are the necessary commands.

pip install requests --upgrade
pip3 install requests --upgrade
python3 -m pip install requests --upgrade

Solution 3 : Try- Except block –

If you are not very sure about the version and cant change the python version configuration then we can use both the syntax in try and except block. Here is the code sample.

try:
  from collections import Mapping
except:
  from collections.abc import Mapping
print(Mapping)
cannot import name 'mapping' from 'collections' solution
cannot import name ‘mapping’ from ‘collections’ solution

 

Most of the solutions are generic for any sub-moduling change. You can apply the same principle incase you want to fix such similar importerrors.

 

Thank
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner