← Back

Home › Errors › ImportError

ImportError: cannot import name — the module was found, the name was not

Exception: ImportErrorCategory: import timeRaised at: the from ... import ... line, before the rest of the module runs

What this error means

This is the sibling of ModuleNotFoundError, and the distinction tells you where to look. ModuleNotFoundError: No module named 'utils' means Python could not find the file. ImportError: cannot import name 'helper' from 'utils' means it found the file, imported it, and the name you asked for is not in it.

The message even prints the file it used, which is the single most useful detail on the line:

Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from utils import helper
ImportError: cannot import name 'helper' from 'utils' (/home/asha/app/utils.py)

Cause 1 — the name is not there, or not spelled that way

Read the path in the error message and open that file. Check the name exists at module level — not nested inside a function or a class — and that the spelling and case match exactly. A name defined inside if name == "main": only exists when the file is run directly, never when it is imported.

# utils.py
def load_config():      # the real name
    ...

# app.py
from utils import load_config   # not loadconfig, not load_configuration

Cause 2 — a circular import

The most common non-obvious cause. a.py imports from b.py, and b.py imports from a.py. Whichever module Python starts with is only partially built when the other one asks it for a name, so the name genuinely is not there yet — even though it is defined a few lines below.

# a.py
from b import beta     # b starts importing...
alpha = 1

# b.py
from a import alpha    # ...a is only half-built: alpha does not exist yet
def beta():
    return alpha
Traceback (most recent call last):
  File "a.py", line 1, in <module>
    from b import beta
  File "/home/asha/app/b.py", line 1, in <module>
    from a import alpha
ImportError: cannot import name 'alpha' from partially initialized module 'a' (most likely due to a circular import)

The phrase partially initialized module confirms it. Three fixes, in order of preference: move the shared code into a third module both can import; import the module and qualify the name (import a then a.alpha, resolved at call time rather than import time); or move the import inside the function that uses it so it runs after both modules are built.

Cause 3 — your own file is shadowing a real module

If you named a file random.py, json.py, email.py or test.py, your file wins over the standard library one, and every name you expect from the real module is missing. The path in the error message gives it away — it points inside your project, not into the Python installation:

ImportError: cannot import name 'choice' from 'random' (/home/asha/app/random.py)

Rename your file, then delete the stale pycache folder so Python stops loading the cached shadow.

Cause 4 — the installed version does not have that name

Library APIs change. A tutorial written for version 2.x may import a name that 3.x renamed or removed, and a virtual environment you forgot to activate pins an old release. Check what you actually have:

import sys, mylib
print(sys.version)
print(mylib.__version__, mylib.__file__)
print([n for n in dir(mylib) if not n.startswith('_')])

dir() lists the real public names in the installed version — compare against the name in your import line.

How to debug it

  1. Read the file path inside the error message. That is the module Python actually loaded — open it, do not assume.
  2. Search that file for def <name> or <name> = at zero indentation. Indented definitions are not importable.
  3. If the message says partially initialized module, you have a circular import — break the cycle with a third module or a deferred import.
  4. Run python -c "import mylib; print(mylib.file)" to catch a shadowing filename.
  5. Confirm the interpreter: which python and pip show <package>. A stale global install next to a fresh virtualenv explains a surprising number of these.

Related messages worth knowing

🇮🇳 Hindi में समझें

ImportError: cannot import name 'x' from 'y' ka matlab hai Python ko file mil gayi, par us file me wo naam nahi hai. (ModuleNotFoundError me file hi nahi milti — dono alag problems hain.) Sabse pehle error message ke andar jo path hai wahi file kholo aur dekho naam zero indentation par defined hai ya nahi — function ke andar bana naam import nahi hota. Doosri badi wajah circular import: agar message me partially initialized module likha hai to a.py aur b.py ek doosre ko import kar rahe hain; shared code ko teesri file me le jao ya import ko function ke andar daalo. Teesri wajah: apni file ka naam random.py ya json.py rakh diya, to wo standard library ko shadow kar deti hai — naam badlo aur pycache delete karo.

ModuleNotFoundError Hindi में पढ़ें →