How to package MATLAB Engine API for Python with PyInstaller?
조회 수: 27 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2023년 11월 1일
편집: MathWorks Support Team
2024년 6월 13일
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?
채택된 답변
MathWorks Support Team
2024년 5월 13일
편집: MathWorks Support Team
2024년 6월 13일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!