How to package MATLAB Engine API for Python with PyInstaller?
34 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 1 Nov 2023
Modifié(e) : MathWorks Support Team
le 13 Juin 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?
Réponse acceptée
MathWorks Support Team
le 13 Mai 2024
Modifié(e) : MathWorks Support Team
le 13 Juin 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!