|
14 | 14 |
|
15 | 15 | __version__ = "0.1.0" |
16 | 16 |
|
17 | | -# Import the main module - try multiple paths to handle different wheel structures |
| 17 | +import importlib.util |
| 18 | +import os |
| 19 | +import sys |
| 20 | + |
18 | 21 | _imported = False |
19 | 22 | _import_error = None |
20 | 23 |
|
21 | | -# Try: isomdl_uniffi/isomdl_uniffi.py (generated by maturin for mixed projects) |
22 | | -if not _imported: |
23 | | - try: |
24 | | - from .isomdl_uniffi import * |
25 | | - _imported = True |
26 | | - except ImportError as e: |
27 | | - _import_error = e |
| 24 | +# The wheel structure has both: |
| 25 | +# - isomdl_uniffi/isomdl_uniffi.py (the Python bindings) |
| 26 | +# - isomdl_uniffi/isomdl_uniffi/ (directory containing the .so) |
| 27 | +# Python prefers packages over modules, so we must explicitly load the .py file. |
28 | 28 |
|
29 | | -# Try: isomdl_uniffi/isomdl_uniffi/isomdl_uniffi.py (nested structure) |
30 | | -if not _imported: |
| 29 | +_module_dir = os.path.dirname(os.path.abspath(__file__)) |
| 30 | +_bindings_path = os.path.join(_module_dir, "isomdl_uniffi.py") |
| 31 | + |
| 32 | +if os.path.exists(_bindings_path): |
31 | 33 | try: |
32 | | - from .isomdl_uniffi.isomdl_uniffi import * |
33 | | - _imported = True |
34 | | - except ImportError: |
35 | | - pass |
| 34 | + spec = importlib.util.spec_from_file_location( |
| 35 | + "_isomdl_uniffi_bindings", _bindings_path |
| 36 | + ) |
| 37 | + if spec and spec.loader: |
| 38 | + _bindings_module = importlib.util.module_from_spec(spec) |
| 39 | + sys.modules["_isomdl_uniffi_bindings"] = _bindings_module |
| 40 | + spec.loader.exec_module(_bindings_module) |
| 41 | + # Export everything from the bindings module |
| 42 | + _all_exports = getattr(_bindings_module, "__all__", []) |
| 43 | + for _name in _all_exports: |
| 44 | + globals()[_name] = getattr(_bindings_module, _name) |
| 45 | + _imported = True |
| 46 | + except Exception as e: |
| 47 | + _import_error = e |
36 | 48 |
|
37 | 49 | if not _imported: |
38 | 50 | raise ImportError( |
|
41 | 53 | f"Please run './python/precommit/build-bindings.sh' first. " |
42 | 54 | f"Original error: {_import_error}" |
43 | 55 | ) |
| 56 | + |
0 commit comments