binary-husky/gpt_academic · error · ImportError

markitdown 库未安装,无法进行转换

Error message

markitdown 库未安装,无法进行转换

What it means

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.

Source

Thrown at crazy_functions/doc_fns/read_fns/markitdown/markdown_reader.py:190

    ) -> str:
        """将 PDF 转换为 Markdown

        Args:
            file_path: PDF 文件路径
            output_path: 输出 Markdown 文件路径,如果为 None 则返回内容而不保存

        Returns:
            str: 转换后的 Markdown 内容

        Raises:
            Exception: 转换过程中的错误
        """
        try:
            path = self._validate_file(file_path)
            self.logger.info(f"处理: {path}")

            if not self.markitdown_available:
                raise ImportError("markitdown 库未安装,无法进行转换")

            # 导入 markitdown 库
            from markitdown import MarkItDown

            # 准备输出目录
            if output_path:
                output_path = Path(output_path)
                output_dir = output_path.parent
                output_dir.mkdir(parents=True, exist_ok=True)
            else:
                # 创建临时目录作为输出目录
                temp_dir = tempfile.mkdtemp()
                output_dir = Path(temp_dir)
                output_path = output_dir / f"{path.stem}.md"

            # 图片目录
            image_dir = output_dir / self.config.image_dir
            image_dir.mkdir(parents=True, exist_ok=True)

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Install into the exact interpreter running the app: <that python> -m pip install markitdown
  2. Verify with the same interpreter: <python> -c "import markitdown; print(markitdown.__version__)"
  3. If already 'installed', reinstall to repair a broken dist: pip install --force-reinstall markitdown
  4. Check the instance flag early (converter.markitdown_available) or fail fast at construction rather than at convert time

Example fix

// before
converter = MarkdownConverter()
converter.convert('paper.pdf')  # ImportError: markitdown 库未安装

// after
converter = MarkdownConverter()
if not converter.markitdown_available:
    raise SystemExit('pip install markitdown before using PDF->Markdown')
converter.convert('paper.pdf')
Defensive patterns

Strategy: validation

Validate before calling

def markitdown_installed() -> bool:
    import importlib.util
    return importlib.util.find_spec('markitdown') is not None

if not markitdown_installed():
    raise SystemExit('Missing dependency: pip install markitdown')

Type guard

def converter_usable(converter) -> bool:
    return bool(getattr(converter, 'markitdown_available', False))

Try / catch

try:
    md = converter.convert(pdf)
except ImportError as e:
    if 'markitdown' in str(e):
        subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'markitdown'])
        md = MarkdownConverter().convert(pdf)  # retry with fresh instance
    else: raise

Prevention

When it happens

Trigger: Importing/constructing MarkdownConverter in an environment where `import markitdown` fails (not installed, wrong interpreter/venv, broken install), then calling convert() on a valid PDF.

Common situations: 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.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/734bbb71e705ec1f. Report an issue: GitHub.