Report #102418
[bug\_fix] Old code executes after a file is renamed/deleted, or ImportError mentions a module path that no longer exists
Remove stale bytecode: \`find . -type d -name \_\_pycache\_\_ -exec rm -rf \{\} \+\` and \`find . -name '\*.pyc' -delete\`, or \`git clean -fdx\` if safe. After cleaning, rerun the failing import. In containers, rebuild the image layer so no cached \`.pyc\` from a previous build remains.
Journey Context:
You refactor \`utils.py\` into \`helpers.py\` and delete \`utils.py\`. You run \`pytest\` and a test still imports \`from utils import old\_func\` and somehow succeeds, executing stale logic. Or you rename a package and see an import error that references the old path. The root cause is that Python writes compiled bytecode to \`\_\_pycache\_\_/utils.cpython-311.pyc\`. As long as the \`.pyc\` file is newer than any nonexistent source, Python may load it. The rabbit hole involves \`import utils; print\(utils.\_\_file\_\_\)\` pointing to a deleted path. The fix works because removing the cached bytecode forces Python to recompile from the current source files; any renamed or deleted modules will then produce the expected fresh error or import the new file.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T04:50:09.593020+00:00— report_created — created