Skip to content

refactor: eliminate sys.path.insert hacks — migrate to package-relative imports #266

Description

@sheepdestroyer

Context

Sourcery flagged in PR #264/#265 that 9 scripts still use sys.path.insert hacks despite scripts/__init__.py and scripts/verification/__init__.py being in place. These hacks mutate global interpreter state, are fragile across environments, and mask import errors.

Current state

Group A — classifier/retry scripts importing scripts.chat_helpers (4 files)

All use identical pattern: sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

File Imports
scripts/retry_errors.py:7 scripts.chat_helpers.parse_chat_response
scripts/reclassify_all.py:11 scripts.chat_helpers.parse_chat_response
scripts/classify_direct.py:7 scripts.chat_helpers.parse_chat_response
scripts/benchmark_classifier.py:10 scripts.chat_helpers.parse_chat_response

Group B — verification scripts importing scripts.chat_helpers (3 files)

Insert the repo root (one level above group A):

File Insert path Imports
scripts/verification/verify_canonical_endpoints.py:22 WORKDIR (repo root) scripts.chat_helpers.parse_chat_response
scripts/verification/verify_ollama_routing.py:8 parent.parent.parent scripts.chat_helpers.parse_chat_response
scripts/verification/verification_helpers.py:4 parent.parent.parent scripts.chat_helpers.parse_chat_response

Group C — scripts importing router.* modules (2 files)

File Insert path Imports
scripts/benchmark_tokens.py:9-10 parent + parent/router router.main.estimate_prompt_tokens, router.main.METADATA_OVERHEAD
scripts/verification/verify_breaker.py:6 parent.parent.parent router.circuit_breaker.get_breaker

Complication — sibling imports with try/except fallback (3 files)

These already use the try: from .X except ImportError: from X pattern for sibling imports, which must be preserved:

File Pattern
scripts/verification/verify_ollama_routing.py:16-18 .verification_helpers fallback
scripts/verification/verify_ollama_cooldown.py:5-7 .verification_helpers fallback
scripts/verification/verify_direct_ollama_cooldown.py:5-7 .verification_helpers fallback

Target state

All 9 scripts should import directly via the package namespace — no sys.path.insert needed — while preserving both python -m scripts.verification.X (package) and python scripts/verification/X.py (CLI) execution modes.

For groups A & B (chat_helpers consumers, 7 files)

Approach: These all already import as from scripts.chat_helpers import parse_chat_response — the sys.path.insert is vestigial once the repo root is on PYTHONPATH. Simply delete the insert line. The repo root (~/dev/LLM-Routing) must be on PYTHONPATH for this to work — which is already true when run from the repo root (cwd is sys.path[0]) or when the dev venv is activated (it adds the repo root via site-packages).

Add the repo root to the dev venv .pth file if not already there:

echo "$HOME/dev/LLM-Routing" > "$HOME/dev/LLM-Routing/.venv/lib/python3.*/site-packages/_llm_routing.pth"

For group C (router imports, 2 files)

scripts/benchmark_tokens.py: already imports as from router.main import ... — just needs the repo root on PYTHONPATH. Delete both sys.path.insert lines.

scripts/verification/verify_breaker.py: already imports as from router.circuit_breaker import ... — same. Delete the sys.path.insert.

Verification

After the refactor, both execution modes must work:

# CLI — run from repo root
cd ~/dev/LLM-Routing && python scripts/verification/verify_canonical_endpoints.py --help
python scripts/retry_errors.py --help
python scripts/benchmark_tokens.py --help

# Package — import works
python -c "from scripts.verification.verify_ollama_routing import send_request"
python -c "from scripts.chat_helpers import parse_chat_response"

Files affected

9 files, ~10 sys.path.insert lines to delete. Net negative diff. No new logic — just wiring cleanup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    code-qualityCode quality improvementjulesIssues for JulesmaintenanceMaintenance and tech debt

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions