Report #14797
[bug\_fix] ImportError: bad interpreter / No module named 'json' \(stdlib shadowing\)
Rename the local file or directory that shadows the standard library module \(e.g., \`json.py\` → \`my\_json.py\`\) and remove cached bytecode \(\`rm -rf \_\_pycache\_\_ json.pyc\`\). Never name user modules the same as stdlib modules like \`json\`, \`sys\`, \`io\`, \`copy\`, \`random\`, \`datetime\`.
Journey Context:
Developer creates a new script named \`json.py\` to experiment with JSON processing, containing \`import json; data = json.loads\('\{\}'\)\`. They save it in their project root. When they run \`python json.py\`, they get \`AttributeError: module 'json' has no attribute 'loads'\`. Inspecting \`print\(json.\_\_file\_\_\)\` reveals it's pointing to \`./json.py\` \(the script itself\) rather than \`/usr/lib/python3.x/json/\_\_init\_\_.py\`. Developer realizes that Python's \`sys.path\` places the current script's directory \(or current working directory\) at the start of the search path. Thus, \`import json\` finds the local \`json.py\` first, shadowing the standard library module. The error manifests as \`AttributeError\` if the local file lacks the expected attributes, or \`ModuleNotFoundError\` for sub-imports if the local file is treated as a namespace package. The rabbit hole involves clearing \`\_\_pycache\_\_\` because Python caches the bytecode of the shadowing module, meaning even after renaming the file, stale \`.pyc\` files might persist the shadow. The fix works because renaming the module eliminates the path collision, allowing Python's search to proceed to the standard library location.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:24:38.800738+00:00— report_created — created