{"record":{"id":"734bbb71e705ec1f","repo":"binary-husky/gpt_academic","slug":"markitdown","errorCode":null,"errorMessage":"markitdown 库未安装，无法进行转换","messagePattern":"markitdown 库未安装，无法进行转换","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"crazy_functions/doc_fns/read_fns/markitdown/markdown_reader.py","lineNumber":190,"sourceCode":"    ) -> str:\n        \"\"\"将 PDF 转换为 Markdown\n\n        Args:\n            file_path: PDF 文件路径\n            output_path: 输出 Markdown 文件路径，如果为 None 则返回内容而不保存\n\n        Returns:\n            str: 转换后的 Markdown 内容\n\n        Raises:\n            Exception: 转换过程中的错误\n        \"\"\"\n        try:\n            path = self._validate_file(file_path)\n            self.logger.info(f\"处理: {path}\")\n\n            if not self.markitdown_available:\n                raise ImportError(\"markitdown 库未安装，无法进行转换\")\n\n            # 导入 markitdown 库\n            from markitdown import MarkItDown\n\n            # 准备输出目录\n            if output_path:\n                output_path = Path(output_path)\n                output_dir = output_path.parent\n                output_dir.mkdir(parents=True, exist_ok=True)\n            else:\n                # 创建临时目录作为输出目录\n                temp_dir = tempfile.mkdtemp()\n                output_dir = Path(temp_dir)\n                output_path = output_dir / f\"{path.stem}.md\"\n\n            # 图片目录\n            image_dir = output_dir / self.config.image_dir\n            image_dir.mkdir(parents=True, exist_ok=True)","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/crazy_functions/doc_fns/read_fns/markitdown/markdown_reader.py#L172-L208","documentation":"Raised as ImportError from MarkdownConverter.convert when self.markitdown_available is False — a flag set once at __init__ time by _check_markitdown_installation() (an import probe of the markitdown package). The check happens after _validate_file, so bad paths are reported first; a valid path then hits the missing-dependency wall.","triggerScenarios":"Importing/constructing MarkdownConverter in an environment where `import markitdown` fails (not installed, wrong interpreter/venv, broken install), then calling convert() on a valid PDF.","commonSituations":"Deploying without requirements installed; multiple virtualenvs where the app runs in the one without markitdown; markitdown installed for a different Python version; partial install missing its deps.","solutions":["Install into the exact interpreter running the app: <that python> -m pip install markitdown","Verify with the same interpreter: <python> -c \"import markitdown; print(markitdown.__version__)\"","If already 'installed', reinstall to repair a broken dist: pip install --force-reinstall markitdown","Check the instance flag early (converter.markitdown_available) or fail fast at construction rather than at convert time"],"exampleFix":"// before\nconverter = MarkdownConverter()\nconverter.convert('paper.pdf')  # ImportError: markitdown 库未安装\n\n// after\nconverter = MarkdownConverter()\nif not converter.markitdown_available:\n    raise SystemExit('pip install markitdown before using PDF->Markdown')\nconverter.convert('paper.pdf')","handlingStrategy":"validation","validationCode":"def markitdown_installed() -> bool:\n    import importlib.util\n    return importlib.util.find_spec('markitdown') is not None\n\nif not markitdown_installed():\n    raise SystemExit('Missing dependency: pip install markitdown')","typeGuard":"def converter_usable(converter) -> bool:\n    return bool(getattr(converter, 'markitdown_available', False))","tryCatchPattern":"try:\n    md = converter.convert(pdf)\nexcept ImportError as e:\n    if 'markitdown' in str(e):\n        subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'markitdown'])\n        md = MarkdownConverter().convert(pdf)  # retry with fresh instance\n    else: raise","preventionTips":["Pin markitdown in requirements and verify with the same interpreter that runs the app","Check the markitdown_available flag right after constructing MarkdownConverter","In deployment scripts, add a startup dependency probe that fails fast with install instructions"],"tags":["dependency","markitdown","import-error","environment","python"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}