Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.
Python agent-skillsai-agentsbook-to-skillcontext-engineeringdocument-processingedtechknowledge-baseknowledge-managementllmpdf-to-markdownragself-studystudy-tools
fix(deps): diagnose a module installed in an isolated environment (#151)
Virgilio Junior98f09592d54f
diff --git a/book_to_skill/dependencies.py b/book_to_skill/dependencies.pyindex 903d7a6..6bef17f 100644--- a/book_to_skill/dependencies.py+++ b/book_to_skill/dependencies.py@@ -5,6 +5,8 @@ import osimport shutilimport subprocessimport sys+from pathlib import Path+from book_to_skill.config import PYTHON_DEPENDENCIES, HTML_EXTENSIONS@@ -72,6 +74,32 @@ def python_module_available(module_name: str) -> bool:return importlib.util.find_spec(module_name) is not None+def isolated_install_hint(module_name: str) -> str | None:+ """Explain a module that is installed as a tool but not importable here.++ pipx — the way Docling's own docs suggest installing it — puts the package+ in its own virtualenv and only the executable on PATH. The module is then+ genuinely not importable from this interpreter, so "✗ python: docling" is+ correct and useless: the user installed it, and we say it is missing.++ Returns a line naming the executable and the interpreter that can import+ it, or None when there is no such executable. Never claims the module is+ available — the parsers import it, so a binary on PATH does not make the+ import work; it only tells us where a working environment is.+ """+ executable = shutil.which(module_name)+ if not executable:+ return None+ # pipx layout: <venv>/bin/<tool> — its sibling `python` can import the module.+ venv_python = Path(executable).resolve().parent / "python"+ where = f"\n {venv_python} scripts/extract.py …" if venv_python.exists() else ""+ return (+ f"a `{module_name}` command exists at {executable}, so it is installed in an "+ f"isolated environment (pipx?).\n Run the extractor with that "+ f"environment's Python, or install it into this one:{where}"+ )++def missing_python_packages(module_names: list[str]) -> list[str]:missing = []for module_name in module_names:@@ -239,6 +267,9 @@ def run_dependency_check() -> int:print(f" {'✓' if ok else '✗'} python: {pip_name}")if not ok:missing_pip_packages.append(pip_name)+ hint = isolated_install_hint(module_name)+ if hint:+ print(f" ↳ {hint}")for cmd, pretty, hint in group["system"]:ok = cmd in system_presentdiff --git a/tests/test_isolated_install_hint.py b/tests/test_isolated_install_hint.pynew file mode 100644index 0000000..29adbec--- /dev/null+++ b/tests/test_isolated_install_hint.py@@ -0,0 +1,78 @@+"""A module installed as a tool (pipx) is not importable, and saying only+"missing" sends the user to reinstall something they already have.++The hint must never claim the module is available: the parsers import Docling,+so an executable on PATH does not make the import work. It only tells us where+an environment that can import it lives.+"""++import sys+from pathlib import Path++ROOT_DIR = Path(__file__).resolve().parent.parent+sys.path.insert(0, str(ROOT_DIR))++from book_to_skill import dependencies+++class TestNoExecutable:+ def test_returns_none_when_nothing_on_path(self, monkeypatch):+ monkeypatch.setattr(dependencies.shutil, "which", lambda _: None)++ assert dependencies.isolated_install_hint("docling") is None+++class TestExecutableFound:+ def test_names_the_executable(self, monkeypatch):+ monkeypatch.setattr(+ dependencies.shutil, "which", lambda _: "/home/u/.local/bin/docling"+ )++ hint = dependencies.isolated_install_hint("docling")++ assert hint is not None+ assert "/home/u/.local/bin/docling" in hint+ assert "isolated environment" in hint++ def test_points_at_the_venv_python_when_it_exists(self, monkeypatch, tmp_path):+ venv_bin = tmp_path / "venvs" / "docling" / "bin"+ venv_bin.mkdir(parents=True)+ (venv_bin / "python").write_text("")+ (venv_bin / "docling").write_text("")+ monkeypatch.setattr(+ dependencies.shutil, "which", lambda _: str(venv_bin / "docling")+ )++ hint = dependencies.isolated_install_hint("docling")++ assert str(venv_bin / "python") in hint+ assert "scripts/extract.py" in hint++ def test_omits_the_interpreter_line_when_there_is_no_sibling_python(+ self, monkeypatch, tmp_path+ ):+ lone = tmp_path / "docling"+ lone.write_text("")+ monkeypatch.setattr(dependencies.shutil, "which", lambda _: str(lone))++ hint = dependencies.isolated_install_hint("docling")++ assert hint is not None+ assert "scripts/extract.py" not in hint++ def test_still_treats_the_module_as_missing(self, monkeypatch, capsys):+ """The parsers import it; a binary on PATH does not make that work.++ The report must keep the ✗ and keep offering to install — the hint adds+ a diagnosis, it does not reclassify the dependency as satisfied.+ """+ monkeypatch.setattr(dependencies.shutil, "which", lambda cmd: (+ "/home/u/.local/bin/docling" if cmd == "docling" else None+ ))+ monkeypatch.setattr(dependencies, "python_module_available", lambda _: False)++ dependencies.run_dependency_check()++ out = capsys.readouterr().out+ assert "✗ python: docling" in out+ assert "isolated environment" in out