Files
balance/dm_imu_pkg/__init__.py
ydy0615 c3dac34dcc feat: add IMU Python package with pybind11 bindings
- Add dm_imu_pkg/__init__.py to dynamically load compiled IMU module during development and installation
- Update .vscode/settings.json to ignore missing CMakeLists for flexible builds
- Generate egg-info metadata and package structure for distribution
2025-12-08 23:54:56 +08:00

57 lines
1.8 KiB
Python

import pathlib
import importlib.util
import sys
def _load_imu_module():
"""
Load the compiled pybind11 module ``imu_py``.
When the package is installed via ``pip install .`` the shared object
will be placed next to this ``__init__.py``. During development it may
still reside in the build directory under ``src/pybind_imu/build``.
"""
# 1. Try to import if the .so is already in the package directory.
try:
from . import imu_py # type: ignore # pylint: disable=import-error
return imu_py
except Exception:
pass
# 2. Fallback: locate the build output relative to the repository root.
possible_paths = [
pathlib.Path(__file__).parent.parent
/ "src"
/ "pybind_imu"
/ "build"
/ "imu_py.cpython-310-aarch64-linux-gnu.so",
# Generic pattern for other Python versions / architectures
pathlib.Path(__file__).parent.parent
/ "src"
/ "pybind_imu"
/ "build"
/ "imu_py.*.so",
]
for p in possible_paths:
# glob pattern handling for the generic case
if "*" in str(p):
matches = list(p.parent.glob(p.name))
if matches:
p = matches[0]
else:
continue
if p.is_file():
spec = importlib.util.spec_from_file_location("imu_py", p)
imu_py = importlib.util.module_from_spec(spec)
spec.loader.exec_module(imu_py) # type: ignore
return imu_py
raise ImportError(
"Unable to locate the compiled 'imu_py' module. "
"Make sure the package is installed with the compiled extension "
"or run the build step before importing."
)
# Expose the DmImu class at package level for convenience
_imu_mod = _load_imu_module()
DmImu = _imu_mod.DmImu