Report #15141
[bug\_fix] Local file shadowing standard library module \(e.g., random.py, json.py\)
Rename the local file to something that does not collide with standard library module names \(e.g., \`my\_random.py\`, \`data.json\` instead of \`json.py\`\). Python adds the script's directory to the front of \`sys.path\` \(as \`''\`\), causing local files to shadow built-in modules during import.
Journey Context:
You create a new script called \`random.py\` to test random number generation. The file contains \`import random; print\(random.randint\(1, 10\)\)\`. You run \`python random.py\` and immediately get \`AttributeError: partially initialized module 'random' has no attribute 'randint'\`. Confused, you add \`print\(random.\_\_file\_\_\)\` and see it points to \`./random.py\` in your current directory. You realize that when you named your script \`random.py\`, Python placed the current directory \(which is \`''\` in \`sys.path\`\) at the beginning of the module search path. When your script executes \`import random\`, Python finds your \`random.py\` \(which is currently being executed and is only partially initialized\) instead of the standard library's \`random\` module. You rename the file to \`dice\_roller.py\`, run it, and it works correctly because \`dice\_roller.py\` does not shadow any stdlib module.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:17:35.253520+00:00— report_created — created