ImportError
PythonERRORRuntime ErrorHIGH confidence

Could not import module or member

What this means

Raised when the `import` statement has trouble loading a module, or when `from ... import` fails to find a name within a module. It's a general-purpose import failure.

Why it happens
  1. 1A circular dependency, where two modules try to import each other.
  2. 2Trying to import a specific name from a module, but that name does not exist in the module.
  3. 3The module is found, but there is an error during its initialization.
How to reproduce

This error occurs when trying to import a specific function `non_existent_function` from the `math` module, where it does not exist.

trigger — this will error
trigger — this will error
from math import non_existent_function

expected output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'non_existent_function' from 'math'

Fix 1

Verify the name exists and check for typos

WHEN You get an error about a name that cannot be imported from a module.

Verify the name exists and check for typos
# Incorrect: from math import non_existent_function
# Correct:
from math import factorial  # 'factorial' is a real function in the math module

Why this works

Correcting the name to one that actually exists in the module's namespace allows the import to succeed.

Fix 2

Restructure code to avoid circular imports

WHEN Modules A and B import each other, causing a loop.

Restructure code to avoid circular imports
# In module_a.py
# from module_b import b_func -> This could cause a cycle if module_b imports module_a

# Refactored solution:
# Move shared dependencies to a third module, or import within a function.
def a_func():
    from module_b import b_func # Local import to break the cycle
    b_func()

Why this works

By moving the import statement inside a function, the import is delayed until the function is called, breaking the circular dependency at module load time.

Code examples
Triggerpython
from math import non_existent  # ImportError: cannot import name
Handle with try/exceptpython
try:
    from mylib import helper
except ImportError:
    def helper(*args): return None  # stub fallback
Avoid by checking module attrspython
import math
if hasattr(math, "factorial"):
    from math import factorial
What not to do

Modifying `sys.path` to fix import issues

While it can work, it makes your project's dependency structure implicit and brittle. It's better to structure your project as a proper package or use editable installs.

Sources
Official documentation ↗

cpython/Python/import.c

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Python errors