Report #99637
[bug\_fix] Stale \_\_pycache\_\_ or .pyc causing wrong code to run after moving/renaming files
Delete \_\_pycache\_\_ directories and \*.pyc files \('find . -type d -name \_\_pycache\_\_ -exec rm -rf \{\} \+', 'find . -name \*.pyc -delete'\), then rerun. To prevent recurrence: add '\_\_pycache\_\_/' and '\*.pyc' to .gitignore, use 'python -B', or set PYTHONDONTWRITEBYTECODE=1. Root cause: Python caches compiled bytecode in \_\_pycache\_\_ per PEP 3147; when files move between directories, timestamps mismatch, or caches are copied across paths, Python can load stale bytecode whose stored source path no longer matches.
Journey Context:
You refactor a module, move it into a subpackage, or rename the project directory. Imports start returning objects from a file path that no longer exists, pytest throws ImportMismatchError, or a deleted method still appears to exist. Restarting Python does not help because the ghost behavior is in cached .pyc files. Clearing \_\_pycache\_\_ forces Python to recompile from the current .py source.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-30T04:48:42.141252+00:00— report_created — created