def format_import_errors(
    packages: Union[str, Sequence[str]],
    purpose: Optional[str] = None,
    throw: Union[bool, Exception] = False,
) -> ImportErrorMessages:
    """Format two messages for missing optional package or bad import from an
    optional package.

    Throws an `ImportError` with the formatted message if `throw` flag is set.
    If `throw` is already an exception, throws that instead after printing the
    message.
    """

    if purpose is None:
        purpose = f"using {packages}"

    if isinstance(packages, str):
        packages = [packages]

    requirements = []
    requirements_pinned = []

    for pkg in packages:
        if pkg in all_packages:
            requirements.append(str(all_packages[pkg]))
            requirements_pinned.append(str(pin_spec(all_packages[pkg])))
        else:
            logger.warning("Package %s not present in requirements.", pkg)
            requirements.append(pkg)

    packs = ",".join(packages)
    pack_s = "package" if len(packages) == 1 else "packages"
    is_are = "is" if len(packages) == 1 else "are"
    it_them = "it" if len(packages) == 1 else "them"
    this_these = "this" if len(packages) == 1 else "these"

    msg = cleandoc(f"""
These {pack_s} {is_are} required for {purpose}:

    {",".join(packages)}

You should be able to install {it_them} with pip:

    ```bash
    pip install {" ".join(map(lambda a: f'"{a}"', requirements))}
    ```
""")

    msg_pinned = cleandoc(f"""
There was a problem using these packages:

    {packs}

They are required for {purpose}. Try installing {this_these} exact {pack_s} with pip:

    ```bash
    pip install {" ".join(map(lambda a: f'"{a}"', requirements_pinned))}
    ```
""")

    if isinstance(throw, Exception):
        print(msg)
        raise throw

    elif isinstance(throw, bool):
        if throw:
            raise ImportError(msg)

    return ImportErrorMessages(module_not_found=msg, import_error=msg_pinned)
Format two messages for missing optional package or bad import from an
    optional package.

    Throws an `ImportError` with the formatted message if `throw` flag is set.
    If `throw` is already an exception, throws that instead after printing the
    message.
    
src/core/trulens/core/utils/imports.py