How to package MATLAB Engine API for Python with PyInstaller?

25 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 13 Mai 2024
Bearbeitet: MathWorks Support Team am 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. 

Weitere Antworten (0)

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by