树莓派5上Pyfmi无法加载示例FMU二进制文件:文件不存在错误
树莓派5上PyFMI加载FMU失败问题排查
问题描述
在树莓派5(运行Raspberry Pi OS)上尝试加载Plecs生成的FMU进行仿真,该功能在Windows桌面环境正常运行,但树莓派上无法加载模型。测试加载PyFMI自带的弹跳球示例FMU时,出现相同错误。
报错信息
~ $ python Python 3.9.18 | packaged by conda-forge | (main, Dec 23 2023, 17:20:25) [GCC 12.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from pyfmi import load_fmu >>> load_fmu("/home/baberg/miniconda3/pkgs/pyfmi-2.13.0-py39hf393ea8_3/lib/python3.9/site-packages/pyfmi/examples/files/FMUs/ME2.0/bouncingBall.fmu") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "src/pyfmi/fmi.pyx", line 8683, in pyfmi.fmi.load_fmu File "src/pyfmi/fmi.pyx", line 7613, in pyfmi.fmi.FMUModelME2.__init__ File "src/pyfmi/fmi.pyx", line 4135, in pyfmi.fmi.FMUModelBase2.__init__ pyfmi.fmi.InvalidBinaryException: The FMU could not be loaded. Error loading the binary. Could not load the FMU binary: /tmp/baberg/JModelica.org/jm_tmps2vkgivo/binaries/linux64/bouncingBall.so: cannot open shared object file: No such file or directory >>>
注:已确认上述报错中提到的.so文件实际存在。
源码追踪
追踪PyFMI源码后发现,InvalidBinaryException由status变量返回错误触发,相关代码片段如下:
#Connect the DLL if _connect_dll: self._log_handler.capi_start_callback(self._max_log_size_msg_sent, self._current_log_size) status = FMIL2.fmi2_import_create_dllfmu(self._fmu, self._fmu_kind, &self.callBackFunctions) self._log_handler.capi_end_callback(self._max_log_size_msg_sent, self._current_log_size) if status == FMIL.jm_status_error: last_error = pyfmi_util.decode(FMIL2.fmi2_import_get_last_error(self._fmu)) if enable_logging: raise InvalidBinaryException("The FMU could not be loaded. Error loading the binary. " + last_error) else: raise InvalidBinaryException("The FMU could not be loaded. Error loading the binary. Enable logging for possibly more information.") self._allocated_dll = 1
环境信息
- Python 3.9.18
- PyFMI 2.13.0
- FMILib 2.4.1
- Cython 3.0.12
- Assimulo 3.7.0
- Conda 25.3.1
排查思路
- 检查架构兼容性:树莓派5为ARM64架构,conda-forge提供的PyFMI包可能针对x86_64编译,导致.so文件无法在ARM环境加载。执行
file /tmp/baberg/JModelica.org/jm_tmps2vkgivo/binaries/linux64/bouncingBall.so查看文件架构信息。 - 检查动态库依赖:使用
ldd /tmp/baberg/JModelica.org/jm_tmps2vkgivo/binaries/linux64/bouncingBall.so命令,查看.so文件的动态依赖是否缺失或版本不匹配,比如GLIBC等系统库的版本是否满足要求。 - 启用详细日志:加载FMU时添加
log_level=7参数,即load_fmu("path/to/your.fmu", log_level=7),获取FMILib加载过程中的详细日志,定位具体失败原因。 - 验证conda包架构:确认conda安装的PyFMI和FMILib是否为ARM64版本。可执行
conda list pyfmi查看包的架构信息,若为x86_64,尝试重新安装指定ARM架构的包:conda install -c conda-forge pyfmi --force-reinstall。 - 尝试系统级安装或源码编译:若conda包存在架构问题,可尝试使用Raspberry Pi OS的包管理器安装依赖(如
apt install python3-pyfmi,若仓库存在),或从源码编译PyFMI和FMILib,确保编译过程使用树莓派本地的编译器和系统依赖。
内容的提问来源于stack exchange,提问作者user420383
相关产品推荐
相关产品推荐

