diagnose-stale-pycache-import-mismatch

Diagnose Python ImportError cases where a symbol cannot be imported even though the source file already defines it; verify live source, interpreter/venv selection, clear stale __pycache__, and rerun targeted imports/tests.

5 stars

Best use case

diagnose-stale-pycache-import-mismatch is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Diagnose Python ImportError cases where a symbol cannot be imported even though the source file already defines it; verify live source, interpreter/venv selection, clear stale __pycache__, and rerun targeted imports/tests.

Teams using diagnose-stale-pycache-import-mismatch should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/diagnose-stale-pycache-import-mismatch/SKILL.md --create-dirs "https://raw.githubusercontent.com/vamseeachanta/workspace-hub/main/.agents/skills/workspace-hub-learned/diagnose-stale-pycache-import-mismatch/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/diagnose-stale-pycache-import-mismatch/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How diagnose-stale-pycache-import-mismatch Compares

Feature / Agentdiagnose-stale-pycache-import-mismatchStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Diagnose Python ImportError cases where a symbol cannot be imported even though the source file already defines it; verify live source, interpreter/venv selection, clear stale __pycache__, and rerun targeted imports/tests.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Diagnose stale __pycache__ import mismatch

Use when Python reports an error like:
- `ImportError: cannot import name 'X' from 'module'`
- but the referenced source file already contains `X`

## Why this matters

This failure is often misdiagnosed as a missing code change. In practice, common causes are:
1. stale `.pyc` bytecode in `__pycache__/`
2. the wrong virtualenv/interpreter
3. a long-lived process still using old imports
4. multiple copies of the repo/module on disk

## Procedure

1. Confirm the symbol exists in the live source file.
   - Read the exact file path shown in the traceback.
   - Verify the function/class really exists there now.

2. Confirm where Python is importing from.
   - Activate the intended environment.
   - Run a tiny import script and print `module.__file__`.
   - Check `hasattr(module, 'symbol')`.

3. Check both common local environments if the repo has more than one.
   - In hermes-agent, both `venv` and `.venv` may exist.
   - Validate imports under each when the failing process is unclear.

4. If source is correct but import still fails, clear local bytecode.
   - Target the module cache first:
     `rm -f __pycache__/module*.pyc`
   - If needed, clear repo caches:
     `find . -path '*/__pycache__/*' -delete`

5. Re-run the minimal import check.
   - Example:
     `python - <<'PY'`
     `import module`
     `print(module.__file__)`
     `print(hasattr(module, 'symbol'))`
     `PY`

6. Re-run targeted regression tests around the affected import path.
   - Prefer the smallest relevant set first, then widen if needed.

## Hermes-agent-specific pattern

In `/home/vamsee/.hermes/hermes-agent`, when a traceback references `utils.py` but a newly added helper is already present in that file:

1. Read `utils.py` and confirm the helper exists.
2. Import it under both environments if both exist:
   - `source venv/bin/activate`
   - `source .venv/bin/activate`
3. Print `utils.__file__` and verify `hasattr(utils, 'helper_name')`.
4. Remove local cached bytecode:
   - `rm -f __pycache__/utils.cpython-311.pyc __pycache__/utils.cpython-313.pyc`
5. Re-run the import and targeted pytest selection.

## Good verification bundle

```bash
source venv/bin/activate
python - <<'PY'
import utils
print(utils.__file__)
print(hasattr(utils, 'base_url_host_matches'))
PY
find . -path '*/__pycache__/*' -delete
pytest -q tests/test_base_url_hostname.py
```

## Decision rule

- If `module.__file__` points somewhere unexpected: fix environment/path selection.
- If `module.__file__` is correct and symbol is missing only before cache clear: stale bytecode was the likely cause.
- If imports succeed in a fresh shell but fail in the original process: restart that long-lived process/session.

## Pitfalls

- Do not assume the traceback path means the running interpreter has reloaded that file.
- Do not stop after reading source; always verify with a live import.
- Do not clear only site-packages caches if the failing module is from the repo root.
- If both `venv` and `.venv` exist, checking only one can hide the real problem.

Related Skills

diagnose

5
from vamseeachanta/workspace-hub

Disciplined diagnosis loop for hard bugs and performance regressions. Reproduce → minimise → hypothesise → instrument → fix → regression-test. Use when user says "diagnose this" / "debug this", reports a bug, says something is broken/throwing/failing, or describes a performance regression.

recover-from-stale-git-locks

5
from vamseeachanta/workspace-hub

Diagnose and recover from stale git lock files caused by crashed background processes

python-import-path-mismatch-debugging

5
from vamseeachanta/workspace-hub

Diagnose and fix ModuleNotFoundError when a package is installed but imports still fail due to environment/path mismatches

python-import-path-debugging

5
from vamseeachanta/workspace-hub

Diagnose ModuleNotFoundError when a package is installed but still fails to import

multi-repo-stale-lock-recovery

5
from vamseeachanta/workspace-hub

Diagnose and recover from stale git lock files in multi-repo workspaces, especially with submodules

multi-layer-stale-state-debugging

5
from vamseeachanta/workspace-hub

Detect and clear stale state persisting across multiple storage layers (auth files, cache, code logic)

git-stale-lock-recovery

5
from vamseeachanta/workspace-hub

Diagnose and recover from git stale lock files and corrupted rebase states in multi-repo workflows

diagnose-venv-shebang-mismatch

5
from vamseeachanta/workspace-hub

Diagnose and fix ModuleNotFoundError caused by entry point shebang pointing outside virtual environment

diagnose-venv-shebang-import-errors

5
from vamseeachanta/workspace-hub

Debugging pattern for ModuleNotFoundError when CLI entry points use wrong Python interpreter

diagnose-venv-shebang-conflicts

5
from vamseeachanta/workspace-hub

Debugging pattern for resolving ModuleNotFoundError when CLI entry points use wrong Python interpreter

diagnose-symlink-shebang-persistence

5
from vamseeachanta/workspace-hub

Fix persistent shebang issues in symlinked CLI entry points that revert after git operations

diagnose-shebang-virtualenv-import-errors

5
from vamseeachanta/workspace-hub

Debugging pattern for ModuleNotFoundError when CLI entry points use wrong Python interpreter