PaddlePaddle/PaddleOCR · error · RuntimeError

PyYAML is required to read inference.yml

Error message

PyYAML is required to read inference.yml

What it means

RuntimeError raised by ocr_reference_run.py when `import yaml` fails while loading a PaddleOCR model's inference.yml. The script needs PyYAML to read Global.model_name from the model config; the error names the missing dependency explicitly.

Source

Thrown at deploy/ios_demo/scripts/ocr_reference_run.py:51

import json
import sys
from pathlib import Path
from typing import Any, Dict, List, Sequence


def _paddleocr_package_root() -> Path:
    return Path(__file__).resolve().parents[3]


def _default_ios_models_root() -> Path:
    return Path(__file__).resolve().parent.parent / "PaddleOCRDemo" / "Models"


def _load_yaml_model_name(path: Path) -> str:
    try:
        import yaml
    except ImportError as exc:
        raise RuntimeError("PyYAML is required to read inference.yml") from exc
    with path.open("r", encoding="utf-8") as f:
        data = yaml.safe_load(f)
    return data["Global"]["model_name"]


def _numpy_to_python(obj: Any) -> Any:
    if obj is None:
        return None
    if hasattr(obj, "tolist"):
        return obj.tolist()
    if isinstance(obj, (list, tuple)):
        return [_numpy_to_python(x) for x in obj]
    if isinstance(obj, dict):
        return {k: _numpy_to_python(v) for k, v in obj.items()}
    elif isinstance(obj, (str, int, float, bool)):
        return obj
    return str(obj)

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Install PyYAML into the active environment: `pip install pyyaml`.
  2. Verify with `python -c "import yaml; print(yaml.__version__)"` in the same interpreter the script uses.
  3. Add pyyaml to the deploy/ios_demo requirements file if it is missing so the env is reproducible.

Example fix

// before
$ python deploy/ios_demo/scripts/ocr_reference_run.py ...
RuntimeError: PyYAML is required to read inference.yml

// after
$ pip install pyyaml
$ python deploy/ios_demo/scripts/ocr_reference_run.py ...
Defensive patterns

Strategy: validation

Validate before calling

def pyyaml_available() -> bool:
    try:
        import yaml  # noqa: F401
        return True
    except ImportError:
        return False

Try / catch

try:
    model_name = _load_yaml_model_name(yml_path)
except RuntimeError as e:
    if "PyYAML" in str(e):
        subprocess.check_call([sys.executable, "-m", "pip", "install", "pyyaml"])
        model_name = _load_yaml_model_name(yml_path)
    else:
        raise

Prevention

When it happens

Trigger: Running the iOS reference OCR script in a Python environment where PyYAML is not installed; a venv created from a partial requirements list; using a stripped-down runtime (e.g. slim docker image) without yaml.

Common situations: Fresh clone plus `pip install -r requirements.txt` where pyyaml is absent; system python vs venv mismatch so a globally installed yaml is not visible.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/d4a684e5c62433f0. Report an issue: GitHub.