Python imported library blocks don't work after a restart.

30 views (last 30 days)
I have imported a python code into a simulink block, and everything works as desired at first. When I restart Matlab, I can still find my library in the library browser, but the block does not work anymore. The error I get is:
Error:MATLAB System block 'Testfile/LibName_Blockname' error occurred when invoking 'setupImpl' method of 'Elementreplacer_VolumeMixer'.
Caused by:
Invalid HandleObjectRef - Entry not found in the server table
when I import the python file again with obj.view, it works again (Untill a restart). Is it possible to keep the file imported? I have to import multiple python files, so having to import them manually every time, is not very efficient.

Answers (1)

Tushar Sharma
Tushar Sharma on 5 Dec 2023
Hi Bastiaan,
If you are experiencing the need to re-import all the files every time you use the custom library, it may indicate that the imported Python functionality is not being fully encapsulated within the custom library. To address this issue, consider the following suggestions:
  • Ensure that all necessary Python files and dependencies are included within the custom library. This includes any modules, scripts, or additional files that are required for the Python functionality. If any dependencies are missing, it can lead to the need for re-importing files.
  • Verify that the paths to the imported Python files and any related dependencies are correctly configured within the custom library. Incorrect paths or missing references can lead to the need for re-importing files.
  • If you have made changes to the Python code or its dependencies, make sure to update the custom library to reflect these changes. This can involve re-packaging the library to include the latest versions of the imported files.
  • Refer the following documentation to reload modified user-defined python module: https://in.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html#buuz303
Also, you may consider going through the following documentation which provides an overview of using Python Importer to implement Python modules or packages in Simulink: https://in.mathworks.com/help/simulink/ug/import-python-code-using-python-importer.html
Hope it helps!
  1 Comment
Bastiaan
Bastiaan on 4 Jan 2024
Thanks for the reply.
I managed to find the issue and it is really simple. Matlabs working directory has to be the folder where the python file is located. A genpath 'addpath(genpath('file'))' does not seem to work. Also when importing the file with the directory added (py.importlib.import_module('Drive/folder/file');) it also does not work. I always have to be in the folder where the file is. This does not fully fix my problem yet, as I would like to be able to store multiple python files in different folder for organisational reasons, but that is the next problem to tackle. If you can help me with that, that would be great.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!