Report #52894
[bug\_fix] ModuleNotFoundError due to local file shadowing stdlib or site-packages
Rename the local file or directory that conflicts with the standard library or installed package \(e.g., rename 'random.py' to 'my\_random.py', or 'json/' directory to 'json\_utils/'\). Delete any corresponding '.pyc' files and '\_\_pycache\_\_' directories to ensure Python does not load the stale bytecode from the old filename.
Journey Context:
Developer creates a new script named 'random.py' to test random number generation. In the same directory, they have another script that imports 'numpy' or uses 'import random' expecting the standard library. When they run the second script, it fails with 'AttributeError: module 'random' has no attribute 'randint'' or 'ModuleNotFoundError: No module named 'json'' \(if they have a 'json.py'\). They check 'random.\_\_file\_\_' and see it points to their local script. They realize that Python's module search path \(sys.path\) includes the current directory \(''\) first. When the script name matches a stdlib module, it shadows it. Renaming the file fixes it, but they must also delete 'random.pyc' in \_\_pycache\_\_ because Python might load that stale bytecode even after renaming the source file if the cache isn't cleared.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T19:16:35.319222+00:00— report_created — created