Skip to content

mistral_common.imports

assert_hf_hub_installed() cached

Raise ImportError if the dependency is missing, with an install hint.

Source code in src/mistral_common/imports.py
@lru_cache()
def assert_hf_hub_installed() -> None:
    r"""Raise ImportError if the dependency is missing, with an install hint."""

    assert_package_installed("huggingface_hub", _get_dependency_error_message("huggingface_hub", "hf-hub"))

assert_jinja2_installed() cached

Raise ImportError if the dependency is missing, with an install hint.

Source code in src/mistral_common/imports.py
@lru_cache()
def assert_jinja2_installed() -> None:
    r"""Raise ImportError if the dependency is missing, with an install hint."""

    assert_package_installed("jinja2", _get_dependency_error_message("jinja2", "guidance"))

assert_llguidance_installed() cached

Raise ImportError if the dependency is missing, with an install hint.

Source code in src/mistral_common/imports.py
@lru_cache()
def assert_llguidance_installed() -> None:
    r"""Raise ImportError if the dependency is missing, with an install hint."""

    assert_package_installed("llguidance", _get_dependency_error_message("llguidance", "guidance"))

assert_opencv_installed() cached

Raise ImportError if the dependency is missing, with an install hint.

Source code in src/mistral_common/imports.py
@lru_cache()
def assert_opencv_installed() -> None:
    r"""Raise ImportError if the dependency is missing, with an install hint."""

    assert_package_installed("cv2", _get_dependency_error_message("opencv", "opencv"))

assert_package_installed(package_name, error_message=None)

Raise ImportError if the package is not importable.

Parameters:

Name Type Description Default
package_name str

The package to check.

required
error_message str | None

Custom error message. If None, a generic message is used.

None

Raises:

Type Description
ImportError

If the package is not installed.

Source code in src/mistral_common/imports.py
def assert_package_installed(package_name: str, error_message: str | None = None) -> None:
    r"""Raise ImportError if the package is not importable.

    Args:
        package_name: The package to check.
        error_message: Custom error message. If `None`, a generic message is used.

    Raises:
        ImportError: If the package is not installed.
    """
    if not is_package_installed(package_name):
        error_message = error_message or f"Package '{package_name}' is required but not installed."
        raise ImportError(error_message)

assert_sentencepiece_installed() cached

Raise ImportError if the dependency is missing, with an install hint.

Source code in src/mistral_common/imports.py
@lru_cache()
def assert_sentencepiece_installed() -> None:
    r"""Raise ImportError if the dependency is missing, with an install hint."""

    assert_package_installed("sentencepiece", _get_dependency_error_message("sentencepiece", "sentencepiece"))

assert_soundfile_installed() cached

Raise ImportError if the dependency is missing, with an install hint.

Source code in src/mistral_common/imports.py
@lru_cache()
def assert_soundfile_installed() -> None:
    r"""Raise ImportError if the dependency is missing, with an install hint."""

    assert_package_installed("soundfile", _get_dependency_error_message("soundfile", "soundfile"))

assert_soxr_installed() cached

Raise ImportError if the dependency is missing, with an install hint.

Source code in src/mistral_common/imports.py
@lru_cache()
def assert_soxr_installed() -> None:
    r"""Raise ImportError if the dependency is missing, with an install hint."""

    assert_package_installed("soxr", _get_dependency_error_message("soxr", "soxr"))

is_hf_hub_installed() cached

Check whether the huggingface_hub package is installed.

Source code in src/mistral_common/imports.py
@lru_cache()
def is_hf_hub_installed() -> bool:
    r"""Check whether the `huggingface_hub` package is installed."""

    return is_package_installed("huggingface_hub")

is_jinja2_installed() cached

Check whether the jinja2 package is installed.

Source code in src/mistral_common/imports.py
@lru_cache()
def is_jinja2_installed() -> bool:
    r"""Check whether the `jinja2` package is installed."""

    return is_package_installed("jinja2")

is_llguidance_installed() cached

Check whether the llguidance package is installed.

Source code in src/mistral_common/imports.py
@lru_cache()
def is_llguidance_installed() -> bool:
    r"""Check whether the `llguidance` package is installed."""

    return is_package_installed("llguidance")

is_opencv_installed() cached

Check whether the cv2 package is importable.

Broken cv2 installs that raise on import are treated as not installed; a warning is logged instead.

Returns:

Type Description
bool

True if cv2 imports cleanly, False otherwise.

Source code in src/mistral_common/imports.py
@lru_cache()
def is_opencv_installed() -> bool:
    r"""Check whether the `cv2` package is importable.

    Broken cv2 installs that raise on import are treated as not installed;
    a warning is logged instead.

    Returns:
        True if `cv2` imports cleanly, False otherwise."""

    try:
        import cv2  # noqa: F401
    except ImportError:
        _cv2_available = False
    except Exception as e:
        # cv2 has lots of import problems: https://github.com/opencv/opencv-python/issues/884
        # for better UX, let's simply skip all errors that might arise from import for now
        _cv2_available = False
        logger.warning(
            f"Warning: Your installation of OpenCV appears to be broken: {e}."
            "Please follow the instructions at https://github.com/opencv/opencv-python/issues/884 "
            "to correct your environment. The import of cv2 has been skipped."
        )
    else:
        _cv2_available = True
    return _cv2_available

is_package_installed(package_name)

Check whether a package is importable in the current environment.

Source code in src/mistral_common/imports.py
def is_package_installed(package_name: str) -> bool:
    r"""Check whether a package is importable in the current environment."""
    return importlib.util.find_spec(package_name) is not None

is_sentencepiece_installed() cached

Check whether the sentencepiece package is installed.

Source code in src/mistral_common/imports.py
@lru_cache()
def is_sentencepiece_installed() -> bool:
    r"""Check whether the `sentencepiece` package is installed."""

    return is_package_installed("sentencepiece")

is_soundfile_installed() cached

Check whether the soundfile package is installed.

Source code in src/mistral_common/imports.py
@lru_cache()
def is_soundfile_installed() -> bool:
    r"""Check whether the `soundfile` package is installed."""

    return is_package_installed("soundfile")

is_soxr_installed() cached

Check whether the soxr package is installed.

Source code in src/mistral_common/imports.py
@lru_cache()
def is_soxr_installed() -> bool:
    r"""Check whether the `soxr` package is installed."""

    return is_package_installed("soxr")