AttributeError: module has no attribute, what it means and how to fix it

The import worked, but the name you asked for is not in the module you got. Two causes dominate to the point of being a running joke among Python programmers: a file of yours named like a standard or library module (random.py, math.py, email.py) sitting next to your script and being imported instead, and a version difference, code written against a library version where the attribute exists, running against one where it was renamed or never existed.

What it looks like

# a file named random.py sits in the same folder:
import random
print(random.randint(1, 10))

Traceback (most recent call last):
  File "main.py", line 2, in <module>
AttributeError: module 'random' has no attribute 'randint'

# the import loaded *your* random.py, which has no randint
print(random.__file__)    # reveals which file was imported

The fix, in order

  1. Print module.__file__ before anything else. It names the exact file imported. If it points into your own folder instead of the library's location, you have shadowing; no other debugging step outranks this one.
  2. Rename your shadowing file (and delete its __pycache__). Files named random.py, math.py, string.py, email.py, json.py, os.py, or the name of any library you use will be picked up when they sit in the script's folder or anywhere earlier on sys.path.
  3. Check for partial, circular imports. A module imported while it is still initializing (A imports B, B imports A) can be visible with only some attributes defined. The attribute exists in the source yet is missing at runtime. Restructure shared code into a third module both can import.
  4. Verify the installed version really has it: dir(module) lists what is actually there, and pip show (or the package's changelog) tells you when the attribute was added, renamed, or removed. Code from tutorials ages; libraries rename things.
  5. Check for attribute-on-the-wrong-object mistakes. os.path needs its import path, NumPy arrays versus lists differ, and classes expose methods on instances, not on the class. dir() on the precise object in the traceback settles which case applies.

If it still happens

If __file__ is right and dir() shows the attribute, yet the error persists, the program is running a different environment than the one you are inspecting (an editor's interpreter versus the terminal's, a service's virtualenv). Print sys.executable in the failing run and compare it with the environment where you checked.

Common questions

It worked yesterday and broke today. What changed? Almost always a new file: someone (or you) added a module named like a standard library or dependency file to the folder, and imports started resolving to it. The timeline fits perfectly, and __file__ proves it in one print.

Why does the attribute exist in the docs but not in my module? Documentation tracks the newest release. If your environment pins an older version, the docs describe a future you have not installed. Match the docs version to your installed version (most doc sites have a version switcher) or upgrade the package deliberately.

Related errors

ImportError · ModuleNotFoundError · AttributeError: 'NoneType' has no attribute