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:
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:
That fails because strptime is a method on the datetime.datetime class, not on the datetime module. The fix is either:
or:
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:
If main.py contains:
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:
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:
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:
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:
The fix is to import the submodule explicitly:
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__, anddir()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.

