Report #68644
[bug\_fix] ImportError or AttributeError due to local file shadowing standard library modules \(e.g., naming a file json.py, random.py, or email.py\)
Rename the offending local file \(e.g., 'json.py', 'random.py'\) or directory \(e.g., 'email/'\) to something that doesn't shadow stdlib modules \(e.g., 'my\_json\_test.py'\). Ensure the script is not being run from inside a package directory containing conflicting names. Use 'python -P' \(Python 3.11\+\) or set 'PYTHONSAFEPATH=1' to prevent adding the current working directory to sys.path\[0\], or explicitly manage PYTHONPATH to exclude the current directory.
Journey Context:
The developer creates a new script named 'json.py' to test JSON parsing, containing 'import json; print\(json.dumps\(data\)\)'. They run 'python json.py' and it works initially because Python puts the script's directory at the front of sys.path, importing the local json.py instead of the stdlib json \(which the local file then tries to import, causing a circular partial initialization\). Later, they move to a different directory and try to run another script that imports the stdlib json module. They get 'AttributeError: partially initialized module 'json' has no attribute 'loads' \(most likely due to a circular import\)' or the local json.py file in the CWD is imported instead of the stdlib module. The traceback shows the path to the local json.py file. The developer checks sys.path and sees '' \(empty string representing CWD\) as the first element. The fix works because Python adds the script's directory \(or CWD when using -c or interactive\) to the front of sys.path. By renaming the file, the import system finds the stdlib module instead. Using -P prevents the unsafe path insertion entirely by not prepending the script's directory to sys.path.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T21:42:13.962063+00:00— report_created — created