Report #45117
[bug\_fix] ModuleNotFoundError or AttributeError when importing stdlib module \(e.g., \`import random\` fails\) after creating a local file named \`random.py\`, \`json.py\`, \`queue.py\`, etc.
Rename the local conflicting file \(e.g., \`random.py\` → \`my\_random\_test.py\`\) and delete the associated \`\_\_pycache\_\_/random.cpython-xxx.pyc\` file to clear the import cache.
Journey Context:
Developer creates a script named \`random.py\` to test Python's random number generation. The file contains \`import random\` followed by \`print\(random.randint\(1, 10\)\)\`. When they run \`python random.py\`, they receive \`AttributeError: partially initialized module 'random' has no attribute 'randint' \(most likely due to a circular import\)\`. Checking \`print\(random.\_\_file\_\_\)\` reveals the path points to their local \`random.py\` rather than the standard library. This occurs because Python inserts the script's directory at the beginning of \`sys.path\` when executing a script directly. Consequently, \`import random\` finds the local file first, which is currently being executed and is only partially initialized, leading to the attribute error. Renaming the file to \`test\_random.py\` and removing the compiled cache resolves the issue because the local shadow is removed, allowing the import system to locate the standard library \`random\` module.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:11:47.532154+00:00— report_created — created