Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ismael-sarmiento/kimera_python/llms.txt

Use this file to discover all available pages before exploring further.

ImportUtils is a static-method utility class that wraps Python’s importlib to provide safe, ergonomic helpers for runtime module management. Dynamic imports are essential in plugin systems where backends are optional — for example, a cache layer that supports Redis, Memcached, or an in-memory fallback should only attempt to import redis or pymemcache when those adapters are actually requested. ImportUtils handles that gracefully, offering existence checks, guarded imports, fully-qualified name resolution, and attribute discovery — all without requiring the caller to manage importlib directly.

Import

from components.tools.utils._import import ImportUtils

module_exists

Checks whether a module can be successfully imported and returns a boolean result. Internally it calls importlib.import_module and catches ModuleNotFoundError, making it safe to call even when the target package is not installed.
@staticmethod
def module_exists(module_name: str, package_name: str = None) -> bool
module_name
str
required
The dotted module name to check, e.g. 'redis' or 'components.tools.hash.md5'.
package_name
str
Optional anchor package for resolving relative imports. Pass this when module_name is a relative reference such as '.sibling_module'. Required when performing a relative import.
Returns: True if the module is importable, False otherwise.
from components.tools.utils._import import ImportUtils

print(ImportUtils.module_exists('json'))      # True
print(ImportUtils.module_exists('hashlib'))   # True
print(ImportUtils.module_exists('notreal'))   # False

raise_exception_if_module_not_exists

A guard helper that combines the existence check with an immediate ModuleNotFoundError raise. Place this at the top of any adapter that depends on an optional package to produce an actionable error message before any attribute access is attempted on a missing module.
@staticmethod
def raise_exception_if_module_not_exists(module_name: str, msg: str = None)
module_name
str
required
The dotted module name that must be importable.
msg
str
Optional custom error message. When omitted, the default message is: "Module '{module_name}' does not exist. Please add it!"
Raises: ModuleNotFoundError if the module cannot be imported.
from components.tools.utils._import import ImportUtils

# Raises: ModuleNotFoundError: Module 'notreal' does not exist. Please add it!
ImportUtils.raise_exception_if_module_not_exists('notreal')

# Custom message
ImportUtils.raise_exception_if_module_not_exists('redis', msg='Install redis: pip install redis')

fullname

Resolves the fully-qualified class name — module.ClassName — for any live Python object. This is useful for serialization, logging, or building registry keys that must survive across process boundaries.
@staticmethod
def fullname(_object: object) -> str
_object
object
required
Any Python object whose class name should be resolved.
Returns: A dotted string such as 'components.tools.hash.md5.build_hash'. For built-in types whose module is builtins, only the class name is returned to avoid builtins.int-style noise.
from components.tools.utils._import import ImportUtils

print(ImportUtils.fullname(42))        # 'int'  (builtin — no module prefix)
print(ImportUtils.fullname('hello'))   # 'str'

class MyClass:
    pass

obj = MyClass()
print(ImportUtils.fullname(obj))  # '__main__.MyClass' (or fully-qualified in a package)

get_parts_of_fullname

Splits a fully-qualified dotted name into its module path and terminal attribute name. The split always occurs at the last dot, so both class names and function names are handled uniformly.
@staticmethod
def get_parts_of_fullname(fullname: str) -> dict
fullname
str
required
A fully-qualified dotted name, e.g. 'components.tools.hash.md5.build_hash'.
Returns: A dict with keys 'module_name' and 'attribute_name' when the input contains at least one dot. Returns None implicitly when the input is a simple name with no dot (i.e. len(parts) <= 1).
from components.tools.utils._import import ImportUtils

parts = ImportUtils.get_parts_of_fullname('components.tools.hash.md5.build_hash')
# {'module_name': 'components.tools.hash.md5', 'attribute_name': 'build_hash'}

parts = ImportUtils.get_parts_of_fullname('build_hash')
# None  — no dot present, no explicit return value
get_parts_of_fullname returns None when the input string contains no dot. Always check the return value before accessing 'module_name' or 'attribute_name' to avoid a TypeError on None.

get_attr_of_module

Dynamically imports a module by dotted name and retrieves a named attribute from it using getattr. This lets you defer imports until they are needed and reference functions or classes by string configuration values.
@staticmethod
def get_attr_of_module(module_name: str, attribute_name: str) -> object
module_name
str
required
The fully-qualified dotted module name to import, e.g. 'components.tools.hash.md5'.
attribute_name
str
required
The name of the attribute to retrieve from the imported module.
Returns: The attribute object — a class, function, constant, or any other module-level name.
from components.tools.utils._import import ImportUtils

fn = ImportUtils.get_attr_of_module('components.tools.hash.md5', 'build_hash')
print(fn({'key': 'value'}))  # MD5 hex string

attribute_exist

A thin wrapper around Python’s built-in hasattr that fits naturally into the ImportUtils API when performing pre-flight checks before dynamic dispatch.
@staticmethod
def attribute_exist(_object_name: object, attribute_name: str) -> object
_object_name
object
required
The object to inspect.
attribute_name
str
required
The attribute name to look up via hasattr.
Returns: True if the object has an attribute with the given name, False otherwise. The return type annotation in source is object, but the underlying hasattr call always returns a bool.
from components.tools.utils._import import ImportUtils

print(ImportUtils.attribute_exist('hello', 'upper'))   # True
print(ImportUtils.attribute_exist('hello', 'encode'))  # True
print(ImportUtils.attribute_exist(42, 'upper'))        # False

Build docs developers (and LLMs) love