Ad Space — Top Banner

ImportError

Python Programming Language

Severity: Moderate

What Does This Error Mean?

An ImportError means Python found the module you are trying to import, but something went wrong while loading it. The most common version is trying to import a specific name from a module that does not have that name. This is slightly different from ModuleNotFoundError, which means the whole module could not be found at all.

Affected Models

  • Python 2.x
  • Python 3.x
  • All Python versions

Common Causes

  • Using 'from module import name' when the name does not exist in that module
  • A typo in the function or class name you are trying to import (names are case-sensitive)
  • Importing a name that was removed or renamed in a newer version of the module
  • Circular imports — two files each trying to import the other, creating an infinite loop
  • The module file has an error in it, which prevents it from loading at all

How to Fix It

  1. Read the error message. If it says 'cannot import name X from Y', then X does not exist in module Y. Check the spelling and case of the name.

    Function and class names are case-sensitive. 'DataFrame' and 'dataframe' are different things.

  2. Look up the module's official documentation or run 'dir(module_name)' in Python to see what names are actually available.

    Example: import math; print(dir(math)) — this shows every function available in the math module.

  3. Check if the module version you have installed matches the version the code was written for. Some names change between versions.

    Run 'pip show module_name' in your terminal to see which version you have installed.

  4. If you see 'cannot import name X' and the name definitely exists, you might have a circular import. Check if file A imports from file B and file B imports from file A.

    To fix circular imports, move the shared code to a third file that both A and B can import from.

  5. If you created a file with the same name as a built-in module (like math.py or os.py), rename your file. Python finds your file first and gets confused.

    This is a very common beginner mistake. Never name your files the same as Python's built-in modules.

When to Call a Professional

ImportErrors are always something you can fix yourself. Check the official documentation for the module to confirm the correct names to import. If it is your own module with a circular import, restructure the code so neither file imports the other.

Frequently Asked Questions

What is the difference between ImportError and ModuleNotFoundError?

ModuleNotFoundError means Python cannot find the module at all — it is not installed. ImportError is the parent error — it covers all import failures, including trying to import a specific name that does not exist in a module. If you see ModuleNotFoundError, install the module. If you see ImportError, the module exists but something inside the import statement is wrong.

Why am I getting ImportError after updating a package?

Package updates sometimes rename or remove functions and classes. A function available in version 1.x might be renamed or moved in version 2.x. Check the package's changelog or migration guide to find the new name for what you are trying to import.

I named my file 'math.py' and now I get ImportError when importing math. Why?

Python searches for modules in the current folder first. If your file is called math.py, Python finds it before it finds the real math module. Rename your file to something else (like my_math.py) and the problem will go away.