AttributeError: module has no attribute (Fix)
Fix the error AttributeError: module has no attribute by checking what module Python imported, whether the attribute name is correct, and whether your own file name is hiding the real module.
Quick fix
import math
print(math.sqrt(9))
# Check what names the module actually has
print(dir(math))
If the attribute is missing, check:
- the spelling
- your
importstatement - whether you named your own file the same as the module
What this error means
This error means:
- Python successfully imported a module
- but Python could not find the attribute name you tried to use
In this error, an attribute can be:
- a function
- a class
- a variable
- a submodule
So if you write something like module_name.some_name, Python expects some_name to exist inside that module. If it does not, you get an AttributeError.
If you want a deeper explanation of attributes on non-module objects, see AttributeError: object has no attribute (Fix).
Simple example that causes the error
import math
print(math.random())
This raises an error because:
- the
mathmodule exists - but
random()is not part ofmath
You would see an error like this:
AttributeError: module 'math' has no attribute 'random'
The correct module here is random, not math:
import random
print(random.random())
Most common causes
The most common reasons are:
- misspelled attribute name
- using the wrong module
- naming your own file the same as a standard library module
- importing the module in a different way than expected
- using code from a different Python version
- trying to use a submodule without importing it correctly
Fix 1: Check the attribute name
A very common cause is simply typing the wrong name.
Python is case-sensitive, so these are different:
math.sqrtmath.Sqrtmath.SQRT
Use dir() explained to see what names a module really has.
import math
print(dir(math))
If you are looking for a specific name:
import math
print("sqrt" in dir(math)) # True
print("random" in dir(math)) # False
This helps you quickly spot:
- spelling mistakes
- wrong capitalization
- names that do not exist in that module
Fix 2: Make sure you imported the correct module
Beginners often expect a function to be in one module when it actually belongs to another.
Example:
random.randint()belongs torandom- not
math
Wrong:
import math
print(math.randint(1, 10))
Correct:
import random
print(random.randint(1, 10))
If you are unsure what a module contains, use help() explained:
import random
help(random)
You can also review how to import a module in Python if import statements still feel confusing.
Fix 3: Check for file naming conflicts
This is one of the most common beginner mistakes.
If your file is named something like:
random.pymath.pyjson.pyos.py
then Python may import your file instead of the real module.
For example, if your script is named random.py:
import random
print(random.randint(1, 10))
you may get an error because Python imported your own random.py file, not the standard library random module.
How to fix it
- Rename your file
Example: changerandom.pytomy_random_test.py - Delete cached files if they exist:
__pycache__/.pycfiles
- Run the script again
This problem is also related to import errors such as ImportError: No module named X and ImportError: cannot import name.
Fix 4: Check what Python actually imported
If you are not sure what Python imported, print the module itself.
import json
print(json)
You might see output like:
<module 'json' from '/Users/you/project/json.py'>
If that path points to your project file, then your file is shadowing the real module.
You can also inspect __file__ directly:
import json
print(json.__file__)
For some modules, __file__ may not exist, so a safer version is:
import json
print(getattr(json, "__file__", "built-in module"))
If the path is wrong, rename the conflicting file and remove __pycache__.
If you want to understand why Python chooses one file over another, read how import works in Python.
Fix 5: Verify your import style
Different import styles give you access to names in different ways.
Style 1: Import the module
import math
print(math.sqrt(16))
Here you must use math.sqrt.
Style 2: Import one name directly
from math import sqrt
print(sqrt(16))
Here you use sqrt directly, not math.sqrt.
A common mistake
from math import sqrt
print(math.sqrt(16))
This fails because you did not import the name math.
Another common mistake:
import math
print(sqrt(16))
This fails because you imported the module, not the name sqrt directly.
Make sure the way you call the name matches the way you imported it.
Fix 6: Check Python version differences
Sometimes the attribute exists only in a different Python version.
This can happen when:
- you copy code from an older tutorial
- you follow documentation for a newer Python version
- a package changed its API between versions
Check your version with:
python --version
If example code does not match your environment, compare:
- your Python version
- the tutorial's Python version
- the package version, if it is not part of the standard library
Debugging steps in order
When you see this error, go through these steps:
- Read the exact module name in the error message
- Read the exact attribute name that Python could not find
- Run
dir(module_name) - Print
module_name - Print
module_name.__file__if available - Look for local files with the same module name
- Confirm that your
importstatement matches how you use the name - Check your Python version if needed
Here are some useful commands:
python --version
python -c "import math; print(dir(math))"
python -c "import json; print(json)"
python -c "import json; print(getattr(json, '__file__', 'built-in module'))"
Common mistakes
These are the most common specific causes:
- Typing the wrong function or attribute name
- Using the wrong module for that function
- Local file name conflicts such as
json.pyorrandom.py - Incorrect import style
- Version mismatch between your Python install and example code
FAQ
Why does this happen even though I installed the module?
The module may be installed correctly, but:
- the attribute name may be wrong
- that attribute may not exist in your version
- a local file with the same name may be hiding the real module
How do I know whether Python imported the wrong file?
Print the module object or module.__file__.
Example:
import json
print(json)
print(getattr(json, "__file__", "built-in module"))
If the path points to your project file, your file is shadowing the real module.
What is an attribute in this error?
An attribute is a name inside an object.
For modules, an attribute can be:
- a function
- a variable
- a class
- a submodule
Can dir() help fix this error?
Yes. dir(module_name) shows the names available on the module.
That helps you find:
- misspellings
- wrong assumptions about the module
- missing attributes
See also
- AttributeError: object has no attribute (Fix)
- ImportError: No module named X (Fix)
- ImportError: cannot import name (Fix)
- How import works in Python
- Python
dir()function explained - Python
help()function explained - How to import a module in Python
If you keep hitting this error, inspect the import step by step. Once you know what Python imported and what names that module really contains, the fix is usually easy to find.