Report #92688
[bug\_fix] ImportError: cannot import name 'X' from 'mypackage' \(stale bytecode\) or ModuleNotFoundError after refactoring
Delete all \`\_\_pycache\_\_\` directories and \`.pyc\` files in the project \(\`find . -type d -name \_\_pycache\_\_ -exec rm -rf \{\} \+\` or \`python -B\` to disable bytecode generation temporarily\). Then restart the Python interpreter to force recompilation from the new source files.
Journey Context:
Developer is refactoring a package, moving \`mypackage/helpers.py\` to \`mypackage/utils/helpers.py\` and updating imports. They run their test suite and get \`ImportError: cannot import name 'old\_function' from 'mypackage.helpers'\`. They check the filesystem and \`mypackage/helpers.py\` has been deleted. They check \`mypackage/\_\_pycache\_\_/helpers.cpython-39.pyc\` and see it still exists and has a recent timestamp from the test run. Python is importing the stale bytecode from the \`\_\_pycache\_\_\` directory for the now-deleted source file. They delete all \`\_\_pycache\_\_\` directories recursively, rerun tests, and the import error disappears as Python now correctly finds the new module structure.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T14:09:54.479535+00:00— report_created — created