Skip to main content
Context-manager helpers for gracefully handling optional import dependencies. Provides import_optional, a context manager that catches ImportError and re-raises it with a human-readable install hint (e.g. pip install <package>[extra]), and nltk_check, a variant tailored to NLTK data-download errors. Used by Granite formatter modules that have optional third-party dependencies.

Functions

FUNC import_optional

import_optional(extra_name: str)
Handle optional imports. Args:
  • extra_name: Package extra to suggest in the install hint (e.g. pip install granite_io[extra_name]).

FUNC nltk_check

nltk_check(feature_name: str)
Variation on import_optional for nltk. Args:
  • feature_name: Name of the feature that requires NLTK, used in the error message.
Raises:
  • ImportError: If the nltk package is not installed, re-raised with a descriptive message and installation instructions.