Report #6913
[bug\_fix] AttributeError: module 'random' has no attribute 'randint' \(or similar\) when importing a stdlib module
Rename the local file or directory that shadows the standard library module \(e.g., 'random.py' -> 'my\_random.py'\) and delete any '\_\_pycache\_\_' directories and '.pyc' files generated from the shadowing file.
Journey Context:
Developer creates a new script to test random numbers and names it 'random.py' in their project root. It contains 'import random' and 'print\(random.randint\(1, 6\)\)'. When they run it, they get AttributeError: module 'random' has no attribute 'randint'. Using 'print\(random.\_\_file\_\_\)' reveals it's pointing to their own 'random.py' instead of the stdlib random module. Developer realizes their file is shadowing the standard library module. They rename it to 'dice.py', but running the script again still fails with the same error. They realize Python compiled the old 'random.py' to 'random.pyc' or '\_\_pycache\_\_/random.cpython-311.pyc' in the same directory, and Python is still picking up that compiled bytecode. They delete '\_\_pycache\_\_' and 'random.pyc', then the import resolves correctly to the stdlib.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:19:06.216985+00:00— report_created — created