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.
# 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
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.__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.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.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 __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.
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.