Report #17303
[bug\_fix] AttributeError: module 'random' has no attribute 'randint' when local file shadows stdlib
Rename the local conflicting file \(e.g., \`random.py\` to \`my\_random.py\`\) and delete the compiled bytecode cache \`\_\_pycache\_\_/random.cpython-\*.pyc\` \(and any \`random.pyc\` in the directory\) to ensure Python doesn't reload the cached shadowing module.
Journey Context:
You create a new script named \`random.py\` in your project root to test random number generation. Inside it, you write \`import random\` then \`print\(random.randint\(1, 10\)\)\`. When you run \`python random.py\`, you get \`AttributeError: module 'random' has no attribute 'randint'\`. This happens because Python adds the script's directory to the front of \`sys.path\`. When \`import random\` executes, Python finds your local \`random.py\` file \(the script itself\) instead of the standard library's \`random\` module. Your \`random.py\` hasn't finished loading \(it hasn't defined \`randint\`\), but the module object exists in \`sys.modules\`, so subsequent imports return this partial module. The fix renames the file to avoid shadowing and crucially requires deleting the \`\_\_pycache\_\_\` directory or \`.pyc\` files. If you only rename the \`.py\` file but leave \`\_\_pycache\_\_/random.cpython-311.pyc\`, Python may still import the cached bytecode from the old shadowing location when you try to import the standard library \`random\` elsewhere, continuing to cause errors until the cache is cleared.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:56:45.593858+00:00— report_created — created