| """ |
| Python 3.13 Compatibility Patch |
| Fixes pkgutil.ImpImporter issues for AI/ML packages |
| """ |
|
|
| import sys |
| import pkgutil |
| import importlib |
| import importlib.util |
| import warnings |
| from typing import Any, Optional, Union |
|
|
|
|
| class ImpImporter: |
| """ |
| Compatibility shim for pkgutil.ImpImporter removed in Python 3.12+ |
| This fixes issues with packages that still try to use the old imp module |
| """ |
| |
| def __init__(self, path: Optional[str] = None): |
| self.path = path |
| |
| def find_module(self, fullname: str, path=None): |
| """Find module using modern importlib instead of deprecated imp""" |
| try: |
| if isinstance(path, list) and path: |
| package = path[0] if path else None |
| else: |
| package = None |
| spec = importlib.util.find_spec(fullname, package) |
| if spec is not None: |
| return self |
| except (ImportError, ValueError, AttributeError): |
| pass |
| return None |
| |
| def load_module(self, fullname: str): |
| """Load module using modern importlib""" |
| try: |
| spec = importlib.util.find_spec(fullname) |
| if spec is not None: |
| module = importlib.util.module_from_spec(spec) |
| sys.modules[fullname] = module |
| if spec.loader: |
| spec.loader.exec_module(module) |
| return module |
| except Exception as e: |
| warnings.warn(f"Failed to load module {fullname}: {e}") |
| raise ImportError(f"No module named '{fullname}'") |
|
|
|
|
| def apply_python313_patches(): |
| """Apply all necessary patches for Python 3.13 compatibility""" |
| |
| |
| if not hasattr(pkgutil, 'ImpImporter'): |
| |
| setattr(pkgutil, 'ImpImporter', ImpImporter) |
| print("β Applied pkgutil.ImpImporter compatibility patch") |
| |
| |
| try: |
| import pkg_resources |
| |
| if not hasattr(pkg_resources, '_initialize_master_working_set'): |
| def _initialize_master_working_set(): |
| """Dummy implementation for missing function""" |
| pass |
| pkg_resources._initialize_master_working_set = _initialize_master_working_set |
| print("β Applied pkg_resources compatibility patch") |
| except ImportError: |
| print("! pkg_resources not available - installing alternative") |
| |
| |
| try: |
| |
| import setuptools._distutils |
| if 'distutils' not in sys.modules: |
| sys.modules['distutils'] = setuptools._distutils |
| print("β Applied distutils compatibility patch") |
| except ImportError: |
| print("! distutils patches not needed") |
| |
| |
| try: |
| import collections |
| from collections.abc import Iterable, Mapping, MutableMapping |
| if not hasattr(collections, 'Iterable'): |
| setattr(collections, 'Iterable', Iterable) |
| setattr(collections, 'Mapping', Mapping) |
| setattr(collections, 'MutableMapping', MutableMapping) |
| print("β Applied collections.abc compatibility patch") |
| except (ImportError, AttributeError): |
| pass |
| |
| |
| try: |
| import numpy as np |
| |
| if not hasattr(np, 'bool'): |
| setattr(np, 'bool', bool) |
| if not hasattr(np, 'int'): |
| setattr(np, 'int', int) |
| if not hasattr(np, 'float'): |
| setattr(np, 'float', float) |
| if not hasattr(np, 'complex'): |
| setattr(np, 'complex', complex) |
| print("β Applied numpy compatibility patch") |
| except ImportError: |
| print("! numpy not available for patching") |
| |
| print("β All Python 3.13 compatibility patches applied successfully") |
|
|
|
|
| if __name__ == "__main__": |
| apply_python313_patches() |
|
|