AttributeError: module 'x' has no attribute 'y'
AttributeError: module 'x' has no attribute 'y' in Python means the module you imported does not have the name you asked for. Often your own file has the same name as the module (random.py, json.py), or the function lives somewhere else inside it.
How do I fix AttributeError: module 'x' has no attribute 'y'?
AttributeError: module 'x' has no attribute 'y' means the module you imported does not contain that name. Most often your own file has the module's name, like random.py, so Python imports your file instead. Rename your file. Otherwise check the spelling and case, or import from the right place: from datetime import datetime.
- 1. Look for a name clashCheck your folder for a file named like the module, such as random.py or json.py, and rename it.
- 2. See what was importedprint(module.__file__) shows which file loaded. print(dir(module)) lists the names it really has.
- 3. Check spelling and nestingmath.sqrt exists but math.Sqrt does not. datetime.now lives in the datetime class, not the module.
What does AttributeError: module 'x' has no attribute 'y' mean?
import random loads the first thing called random that Python finds, and it checks your own folder first. If you saved your script as random.py, Python imports your file instead of the real module, and random.randint does not exist in it. Python 3.13 and newer add a hint telling you to rename the file. Other causes are a typo or wrong capital letter, and names that are inside a class in the module, like datetime.datetime.now.
Why does AttributeError: module 'x' has no attribute 'y' happen?
1. Your file has the same name as the module
Python finds your random.py before the real one. Rename your file, and delete any __pycache__ folder next to it.
Breaks
# file saved as random.py
import random
print(random.randint(1, 6))Works
# file renamed to dice.py
import random
print(random.randint(1, 6))2. The name is inside a class, not the module
The datetime module contains a class also called datetime. now() belongs to the class.
Breaks
import datetime
print(datetime.now())Works
from datetime import datetime
print(datetime.now())3. A typo or wrong capital letters
Python names are case-sensitive: math.sqrt exists, math.Sqrt does not.
Breaks
import math
print(math.Sqrt(16))Works
import math
print(math.sqrt(16))4. Two files that import each other
If a.py imports b.py and b.py imports a.py, one of them is only half loaded. Older Python, and files inside a package, say "partially initialized module". Python 3.13 and newer often show a "consider renaming" hint instead, which is misleading here. Move the shared code into a third file.
Breaks
# a.py
import b
def helper(): ...
# b.py
import a
a.helper()
# run: python b.pyWorks
# common.py
def helper(): ...
# a.py and b.py
import common
common.helper()How to find it in your own code
Checklist: 1) Look at your folder for a file with the same name as the module, and rename it. 2) print(module.__file__) to see which file was really imported. 3) print(dir(module)) to list the names it does have. 4) Check the spelling and case in the module's documentation. 5) If the message says "partially initialized", or suggests renaming a file you did not name after any library, look for two files importing each other.
Practise scope and closure bugs
None of the challenges raises this exact error yet. These practise the same kind of bug.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.