Report #103746
[bug\_fix] Stale bytecode in \_\_pycache\_\_ causes AttributeError or ImportError after moving or deleting source files
Delete all \`\_\_pycache\_\_\` directories and \`\*.pyc\` files in the project, then rerun. For debugging sessions, launch Python with the \`-B\` flag or set \`PYTHONDONTWRITEBYTECODE=1\` to suppress bytecode generation. Do not commit \`\_\_pycache\_\_\` to version control.
Journey Context:
You refactor a package: rename \`utils.py\` to \`helpers.py\` and delete an old submodule. When you run tests, you get AttributeError on an attribute that no longer exists, or ImportError referring to a file you already deleted. The source tree is clean, but the traceback points to paths inside \`\_\_pycache\_\_\`. Python's import machinery uses timestamp- and hash-based invalidation of \`.pyc\` files, but in some cases \(for example, when a \`.pyc\` is loaded from a path still reachable in another directory, or when filesystem clocks are skewed\) stale bytecode is executed. The fix is to purge cached bytecode and, during active refactoring, run with \`-B\` or \`PYTHONDONTWRITEBYTECODE=1\` so only source files are considered.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-13T04:37:47.439830+00:00— report_created — created