← Back

Home › Errors › AttributeError

AttributeError: module has no attribute

Exception: AttributeErrorCategory: runtime (attribute lookup)Raised at: the moment a name is looked up on a module object

What this error means

Python found the module and imported it successfully, then looked for a name on it that is not there. The message tells you which module object was searched, including its file — read that path before anything else:

Traceback (most recent call last):
  File "app.py", line 3, in <module>
    print(json.load)
AttributeError: module 'json' has no attribute 'load'

Cause 1 — you imported the package, not the submodule

The most common cause with the standard library and large packages. import os.path and import xml.etree.ElementTree make the submodule available, but a bare import xml does not, because submodules are only loaded when something asks for them:

import xml
xml.etree.ElementTree.parse("a.xml")   # AttributeError: no attribute 'etree'

import xml.etree.ElementTree as ET     # loads the submodule explicitly
ET.parse("a.xml")                      # works

The same applies to third-party packages: import scipy does not load scipy.stats. Import the submodule you actually use.

Cause 2 — a circular import left the module half-built

If a.py imports b.py and b.py imports a.py, one of them is only partially executed when the other reaches for an attribute on it. The attribute exists in the file — it just has not been defined yet:

# a.py
import b            # b starts executing...
ALPHA = 1

# b.py
import a
print(a.ALPHA)      # AttributeError: module 'a' has no attribute 'ALPHA'

Three fixes: move the shared code into a third module both can import; import the module and defer the lookup into a function so it happens at call time; or move the import inside the function that needs it.

Cause 3 — your own file is shadowing the real module

If you named a file json.py, random.py, email.py or test.py, your file wins over the standard library one. The file path in the error message gives it away — it points into your project, not into the Python installation:

AttributeError: module 'json' has no attribute 'loads' (/home/asha/app/json.py)

Rename your file, then delete the stale pycache folder so the cached bytecode is not reused:

python -c "import json; print(json.__file__)"   # should point into site-packages
rm -rf __pycache__ .pytest_cache

Cause 4 — the name is not in this version

Library APIs change. A name added in version 2.x simply does not exist in the 1.x you have installed, and a forgotten virtualenv is the usual reason. Ask the module itself what it has:

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

How to debug it

  1. Read the file path in the error message. That is the module Python actually loaded — open it rather than assuming.
  2. Run python -c "import mylib; print(mylib.file)" to catch a shadowing filename in your project.
  3. Run dir(mylib) and compare against the name you asked for. A typo or a renamed API shows up immediately.
  4. If the module is one of your own, check for a circular import — move the import inside the function as a quick test.
  5. For packages, import the submodule explicitly. import scipy.stats rather than import scipy.
  6. Delete pycache after renaming a file, so stale bytecode cannot keep the old module alive.

Related messages worth knowing

🇮🇳 Hindi में समझें

AttributeError: module 'x' has no attribute 'y' ka matlab hai Python ko module mil gaya, par usme wo naam nahi hai. Sabse pehle error message me jo file path hai use padho — wahi batata hai ki asal me kaun-si file load hui. Chaar wajah: (1) aapne package import kiya, submodule nahi — import xml se xml.etree available nahi hota, import xml.etree.ElementTree likho; (2) circular import — do files ek doosre ko import kar rahi hain, isliye ek adhoori hai jab doosri usse attribute maangti hai; (3) aapki apni file ka naam json.py ya random.py hai, to wo standard library ko shadow kar deti hai — naam badlo aur pycache delete karo; (4) installed version me wo naam hai hi nahi — dir(mylib) se check karo.

ModuleNotFoundError Hindi में पढ़ें →