Pyinstaller with MATLAB engine

17 views (last 30 days)
Xianjie
Xianjie on 10 May 2023
Commented: Grace Kepler on 14 Jun 2024
Hello,
I am working on a project which uses Python to create a GUI for a MALTAB project. I have created a Conda environment and the GUI project works well in this environment. Then I try to use Pyinstaller to encapsulate this project and there are some problems with the MATLAB engine.
Details of this project: MATLAB: 2022b; Python: 3.8; OS: Ubuntu: 22.04
The error message is as the following:
Could not initialize type.
Traceback (most recent call last):
File "readfile_main.py", line 6, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "PyInstaller/loader/pyimod02_importers.py", line 500, in exec_module
File "matlab/__init__.py", line 53, in <module>
from matlabmultidimarrayforpython import
SystemError: initialization of matlabmultidimarrayforpython raised unreported exception
[2155559] Failed to execute script 'readfile_main' due to unhandled exception!
The Python version and MATLAB version are a good match, and I can find all the necessary files, such as 'matlabmultidimarrayforpython.so' under my matlab installation directory ('/usr/local/MATLAB/R2022b/extern/bin/glnxa64/'). I have everything the MATLAB engine needs.
Also, I found some error messages about the modules PyInstaller was not able to find that appear in the Pyinstaller log ('warn-readfile_main.txt'):
missing module named pyimod02_importers - imported by /home/xdai/anaconda3/envs/gui_2022/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_pkgutil.py (top-level)
missing module named matlabmultidimarrayforpython - imported by matlab (top-level), /home/xdai/Desktop/gui_exe/readfile_main.py (top-level)
missing module named _scproxy - imported by urllib.request (conditional)
missing module named StringIO - imported by matlab.engine.matlabengine (optional)
missing module named matlab.engine.MatlabExecutionError - imported by matlab.engine (top-level), matlab.engine.matlabengine (top-level)
missing module named matlab.engine.CancelledError - imported by matlab.engine (top-level), matlab.engine.matlabfuture (top-level)
missing module named matlab.engine.TimeoutError - imported by matlab.engine (top-level), matlab.engine.matlabfuture (top-level), matlab.engine.fevalfuture (top-level)
I would appreciate it a lot if you can help me with this problem. If you need more information, please let me know.
Thanks for the help in advance!

Accepted Answer

Shubham
Shubham on 31 May 2023
Hi Xianjie,
Based on the error message you've provided, it seems like PyInstaller is not able to find the necessary modules for the MATLAB engine to work properly. Specifically, it can't find the matlabmultidimarrayforpython module, which is needed for multidimensional array support in MATLAB.
To resolve this issue, you need to make sure that PyInstaller includes all the required modules in the final executable. One way to do this is by adding the path to the matlabmultidimarrayforpython.so file in the PyInstaller spec file. Here's how you can do that:
  1. First, generate a spec file for your project by running
pyinstaller readfile_main.py --name myapp --onefile.
2. Edit the spec file (myapp.spec) and add the following code in the Analysis section, after the line that starts with datas=[]:
a.binaries += [
('/usr/local/MATLAB/R2022b/extern/bin/glnxa64/matlabmultidimarrayforpython.so', '.'),
]
Replace /usr/local/MATLAB/R2022b/extern/bin/glnxa64 with the correct path to your matlabmultidimarrayforpython.so file.
3. Build the executable again using the modified spec file:
pyinstaller myapp.spec.
This should include the required modules in the final executable, allowing the MATLAB engine to work properly. If you're still having issues, you may need to add other missing modules to the PyInstaller spec file.
  2 Comments
Xianjie
Xianjie on 2 Jun 2023
Thanks so much for your answer. I had moved my project to windows OS and things work well.
Grace Kepler
Grace Kepler on 14 Jun 2024
For more detailed instructions on using Pyinstaller with MATLAB Python engine, see this MATLAB Answer.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!