lharri73 / PySimlink

Compile, run, and interact with Simulink models natively in Python. https://lharri73.github.io/PySimlink/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failed to load compiled model

Thandre opened this issue · comments

I try to load a test model into pysimlink. I do a simple:

model = Model("testmodel", "./testmodel.zip", generator='MinGW Makefiles')

After compile step I get an error loading the generated module:

(_venv) D:\Python\pySimlink>python main.py
Compiling ... done
Traceback (most recent call last):
File "D:\Python\pySimlink\main.py", line 14, in
model = Model("testmodel", "./testmodel.zip", generator='MinGW Makefiles')
File "D:\Python\pySimlink_venv\lib\site-packages\pysimlink\lib\model.py", line 92, in init
self.module = importlib.import_module(self.model_paths.module_name)
File "C:\Program Files\Python39\lib\importlib_init
.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 666, in _load_unlocked
File "", line 565, in module_from_spec
File "", line 1173, in create_module
File "", line 228, in _call_with_frames_removed
ImportError: DLL load failed while importing testmodel_interface_c: Das angegebene Modul wurde nicht gefunden.<

I set up a virtual environment in Python 3.9.9 with just the requirements needed for pysimlink. I also had to comment out the two lines for locking / unlocking as described in #17 .

The file "D:\Python\pySimlink\__pycache__\pysimlink\testmodel\build\out\library\testmodel_interface_c.cp39-win_amd64.pyd" is generated and is located in the given folder.

When I try the steps with Python 3.6 everything works fine.
It seems to be a Windows specific issue that was introduced in Python 3.8 (Link).

Does adding os.add_dll_directory(dir) to the for loop at model.py fix the issue? If so, I can push a fix.

from https://stackoverflow.com/a/67437837

This was also my first thought. Unfortunately this doesn't solve the issue. Maybe the path has to be adjusted as well. According to this comment the backslashes have to be replaced with slashes and double slash after the colon. I'll try this and give an update.

It seems to be also an issue with MinGW compiler. The "bin" folder of the MinGW installation has to be added with os.add_dll_directory() as well.
I found the information here and here.
The original problem is that MinGW has its own libstdc++-6.dll which is dynamically linked to the compiled Simulink model. This makes it necessary to add the MinGW bin folder to the "dll search path". As an alternative the Simulink model has to be linked with static libs but I don't know how to do that.