Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Fix: ImportError: attempted relative import with no known parent package
Next article icon

Python Importerror: Cannot Import Name Mapping From Collections

Last Updated : 12 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with Python, encountering ImportError can be frustrating, especially when it involves the inability to import a specific name like 'Mapping' from the 'Collections' module. This error typically occurs when there is a mismatch in the Python version or a codebase that relies on deprecated functionality. In this article, we'll explore the reasons behind the ImportError.

What is ImportError: Cannot Import Name 'Mapping' From 'Collections'?

The ImportError: Cannot Import Name 'Mapping' From 'Collections' occurs when there is a problem importing the 'Mapping' class from the 'Collections' module. This error often arises due to changes in Python versions or when code relies on deprecated features.

Syntax:

ImportError: Cannot Import Name 'Mapping' From 'Collections'

Why does ImportError: Cannot Import Name 'Mapping' From 'Collections' Occur?

Below, are the reasons for ImportError: Cannot Import Name 'Mapping' From 'Collections' occurring.

  • Python Version Mismatch
  • Outdated Code Reliance
  • Circular Imports

Python Version Mismatch

Incompatibility between the codebase and the Python version being used can lead to the ImportError. The 'Mapping' class has been deprecated in recent Python versions, and attempts to import it may result in an error.

Python3
# Python 2.x code from collections import Mapping  # Python 3.x does not have Mapping class in collections 

Output:

Solution.py:2: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,
and in 3.9 it will stop working
from collections import Mapping

Outdated Code Reliance on Deprecated Functionality

If your codebase relies on deprecated features, it may encounter this ImportError. In the example below, the code is trying to import 'Mapping' from 'collections,' but the class has been removed in Python 3.

Python3
# Outdated code relying on deprecated functionality from collections import Mapping  # Use the 'collections.abc' module for a more updated approach from collections.abc import Mapping 

Output

Solution.py:2: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 
3.9 it will stop working
from collections import Mapping

Circular Imports

Circular imports, where two or more modules depend on each other, can lead to ImportError. If one of these modules imports 'Mapping' from 'collections,' it can result in the error.

Python3
# Module A from collections import Mapping from module_b import some_function  # Module B from module_a import some_other_function 

Output :

Hangup (SIGHUP)
Solution.py:2: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in
3.9 it will stop working
from collections import Mapping
Traceback (most recent call last):
File "Solution.py", line 3, in <module>
from module_b import some_function
ModuleNotFoundError: No module named 'module_b'

Approaches to Solve with Correct Code

Below, are the Approaches to Solve Importerror: Cannot Import Name ‘Mapping’ From ‘Collections’.

  • Update Code for Python 3
  • Check for Circular Imports
  • Verify Python Version

Update Code for Python 3 Compatibility

Ensure that your code is compatible with Python 3 by replacing the deprecated 'Mapping' class with the updated 'collections.abc.Mapping' class.

Python3
# Update code for Python 3 compatibility from collections.abc import Mapping 

Check for Circular Imports

Identify and resolve any circular imports within your code. Reorganize your modules or use conditional imports to break the circular dependency.

Python3
# Module A from module_b import some_function  # Module B from module_a import some_other_function 

Verify Python Version

Ensure that your Python environment is using a compatible version. If you're working with legacy code, consider upgrading it to use more recent and supported Python versions.

# Verify and update Python version
python --version

Conclusion

In Conclusion, The ImportError: Cannot Import Name 'Mapping' From 'Collections' can be resolved by updating code for Python 3 compatibility, addressing circular imports, and ensuring the correct Python version. By following the provided approaches and code examples, you can troubleshoot and fix this common issue, allowing your code to run smoothly across different Python environments.


Next Article
How to Fix: ImportError: attempted relative import with no known parent package

K

kasoti2002
Improve
Article Tags :
  • Python
  • Python Programs
  • Python Errors
Practice Tags :
  • python

Similar Reads

  • How to Fix ImportError: Cannot Import name X in Python
    We are given an error "Importerror: Cannot Import Name ‘X’ From ‘Collections’ " in Python and our task is to find the solution for this error. In this article we will see the reasons for occurring and also the solution for the Importerror: Cannot Import Name ‘X’ From ‘Collections’ " error in Python.
    3 min read
  • How to Fix: ImportError: attempted relative import with no known parent package
    ImportError: attempted relative import with no known parent package error occurs when attempting to import a module or package using a relative import syntax, but Python is unable to identify the parent package. In this article, we will see how to solve this error in Python. What is ImportError: Att
    3 min read
  • Python Code Error: No Module Named 'Aiohttp'
    Python is most favourite and widely used programming language that supports various libraries and modules for different functionalities In this article, we are going to see how we can fix the Python Code Error: No Module Named 'Aiohttp'. This error is encountered when the specified module is not ins
    3 min read
  • ModuleNotFoundError: No module named 'dotenv' in Python
    The ModuleNotFoundError: No module named 'dotenv' error is a common hurdle for Python developers dealing with environment variables. This glitch arises when the interpreter can't find the indispensable "dotenv" module. In this brief guide, we'll uncover the reasons behind this issue, outline common
    3 min read
  • How to Import Other Python Files?
    We have a task of how to import other Python Files. In this article, we will see how to import other Python Files. Python's modular and reusable nature is one of its strengths, allowing developers to organize their code into separate files and modules. Importing files in Python enables you to reuse
    3 min read
  • ModuleNotFoundError: No module named 'celery' in Python
    In Real-Time application development, many Programmers and Developers prefer Python language due to its enhanced features and support for different modules and packages. When developers work on various modules they commonly encounter the ModuleNotFoundError: No module named 'celery' error. In this a
    2 min read
  • ModuleNotFoundError: No module named Error in Python
    The "No Module Named..." error is raised when Python attempts to import a module that it cannot locate. Modules are essentially Python files containing functions and variables that can be reused in different parts of a program. When Python encounters an import statement, it searches for the specifie
    2 min read
  • Filenotfounderror: Errno 2 No Such File Or Directory in Python
    When working with file operations in programming, encountering errors is not uncommon. One such error that developers often come across is the FileNotFoundError with the Errno 2: No such file or directory message. This error indicates that the specified file or directory could not be found at the gi
    3 min read
  • AttributeError: can’t set attribute in Python
    In this article, we will how to fix Attributeerror: Can'T Set Attribute in Python through examples, and we will also explore potential approaches to resolve this issue. What is AttributeError: can’t set attribute in Python?AttributeError: can’t set attribute in Python typically occurs when we try to
    3 min read
  • Output of Python programs | Set 9 (Dictionary)
    Prerequisite: Dictionary 1) What is the output of the following program? [GFGTABS] Python dictionary = {'GFG' : 'geeksforgeeks.org', 'google' : 'google.com', 'facebook' : 'facebook.com' } del dictionary['google']; for key, values in dictionary.
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences