Python
AttributeError
Troubleshooting
Debugging
Programming Tips

AttributeError 'module' object has no attribute

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

AttributeError: module 'x' has no attribute 'y' means Python successfully imported a module object, but the name you are trying to access is not defined on that module. The error sounds simple, but the root cause is often one of a handful of recurring problems: wrong import target, local file shadowing, circular imports, or library API changes.

Start By Inspecting What Python Actually Imported

Before changing code blindly, print the module object and its origin:

python
1import some_module
2
3print(some_module)
4print(getattr(some_module, "__file__", "built-in"))
5print(dir(some_module))

This tells you whether Python imported the package you expected or a different file with the same name. That one check resolves a large percentage of cases.

Wrong Import Target

A classic example is datetime:

python
import datetime

datetime.strptime("2025-01-01", "%Y-%m-%d")

That fails because strptime is a method on the datetime.datetime class, not on the datetime module. The fix is either:

python
1import datetime
2
3parsed = datetime.datetime.strptime("2025-01-01", "%Y-%m-%d")
4print(parsed)

or:

python
1from datetime import datetime
2
3parsed = datetime.strptime("2025-01-01", "%Y-%m-%d")
4print(parsed)

The same pattern appears with os.path, xml, matplotlib, and many other modules that expose classes or submodules rather than top-level functions.

Local File Shadowing

Another very common cause is naming one of your own files after a standard library or third-party package:

text
project/
  requests.py
  main.py

If main.py contains:

python
import requests

print(requests.get("https://example.com"))

Python may import your local requests.py file instead of the installed requests package. Then requests.get does not exist, and you get the attribute error.

The fix is to rename the local file and remove stale bytecode caches if needed:

bash
find . -name '__pycache__' -type d -prune -exec rm -rf {} +

After renaming, run the import check again and confirm that module.__file__ points to the intended package.

Circular Imports

Circular imports can leave a module only partially initialized. That means names you expect to exist may not be defined yet.

For example:

python
1# a.py
2import b
3
4value = 42
python
1# b.py
2import a
3
4print(a.value)

Depending on import order, a.value may not exist yet when b.py tries to access it. The fix is usually to move shared code into a third module or delay the import until runtime inside a function.

Package API Changes

Sometimes your code follows old documentation or examples from another version of the library. In that case, the module is correct but the attribute name no longer exists.

Check the installed version:

bash
pip show some-package

Then compare that version with the documentation you are using. This matters a lot in libraries such as TensorFlow, pandas, and SQLAlchemy, where public APIs changed substantially across major versions.

Package Versus Submodule Confusion

A package may not automatically import all of its submodules. This can lead to errors like:

python
import xml

xml.etree.ElementTree.parse("data.xml")

The fix is to import the submodule explicitly:

python
1import xml.etree.ElementTree as ET
2
3tree = ET.parse("data.xml")
4print(tree.getroot().tag)

Do not assume that importing the top-level package exposes every child module as an attribute.

Common Pitfalls

The biggest mistake is staring at the attribute name and ignoring the imported module object. Print the module path first. If the wrong file was imported, every later debugging step will waste time.

Another pitfall is leaving a renamed file's __pycache__ behind and assuming the problem persists for mysterious reasons. Clean caches after renaming shadowing files.

Developers also often fix the symptom by adding more imports without understanding whether the issue is a class, module, or package boundary problem. That can make the import graph messier.

Finally, be suspicious of examples copied from old blog posts. Attribute errors are frequently version mismatches wearing a generic Python exception message.

Summary

  • This error means Python imported a module, but the expected attribute is not present on it.
  • Print the module object, its __file__, and dir() output first.
  • Check for wrong import targets, local file shadowing, circular imports, and API changes.
  • Import submodules explicitly when package boundaries matter.
  • Fix the root import problem instead of piling on extra imports.

Course illustration
Course illustration

All Rights Reserved.