How to package MATLAB Engine API for Python with PyInstaller?
29 views (last 30 days)
Show older comments
MathWorks Support Team
on 1 Nov 2023
Edited: MathWorks Support Team
on 13 Jun 2024
I am using PyInstaller to package my Python application, which uses the MATLAB Engine API for Python. The executable file that is created does not bundle the "matlab" Python package. As a result, when the executable file is launched, I get the following error.
ModuleNotFoundError: No module named 'matlab'
How can I package MATLAB Engine API for Python with PyInstaller?
Accepted Answer
MathWorks Support Team
on 13 May 2024
Edited: MathWorks Support Team
on 13 Jun 2024
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!