Follow the steps below to include the MATLAB Engine API package in your app.
1. Navigate to the directory containing the Python target file for PyInstaller, and create a directory named "hooks", if it does not already exist.
2. Inside the "hooks" directory, create a Python script called "hook-matlab.py" with the following code for importing the "matlab" package:
from PyInstaller.utils.hooks import collect_all
datas, binaries, hiddenimports = collect_all('matlab')
3. Package the Python application using PyInstaller with the "--additional-hooks-dir" option.
pyinstaller <Python target file> --additional-hooks-dir=hooks
Additional Troubleshooting
Case 1:
MATLAB Python engine not installed in a default location
You will need to add path information when running the pyinstaller command.
pyinstaller <Python target file> --additional-hooks-dir=hooks --paths <path to the "matlab" folder>
Case 2: MATLAB R2024a or later
If you receive the following error
ImportError: DLL load failed while importing matlabmultidimarrayforpython: The specified module could not be found.
the path to the "matlabmultidimarrayforpython" library needs to be included in the "paths". For example,
pyinstaller <Python target file> --additional-hooks-dir=hooks --paths "C:\Program Files\MATLAB\R2024a\extern\bin\win64"
Case 3: Install Corrupted Error
If you receive the following error when running your executable,
the MATLAB Engine for Python install is corrupted
there is an issue with the "hooks" folder setup. For example, the hooks folder doesn't exist, the "--additional-hooks-dir=hooks" flag is not used or the "hook-matlab.py" file doesn't exist.