Pyinstaller matlab-engine problem running the .exe on a different machine

6 次查看(过去 30 天)
I'm working on an application that uses the matlab engine API for python. The app works fine, without any issue, therefore I wanted to pack it into a single exe.
At first I had an issue given by mlarray, that I solved by compiling with:
pyinstaller --additional-hook-dir=hooks --onefile main.py
Now on my own computer the .exe file works fine, but as soon as I try to run it on a different machine (with Matlab installed), it gives me the following error:
Traceback (most recent call last):
File "matlab\engine\__init__.py", line 45, in <module>
File "importlib\__init__.py", line 127, in import_module
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'matlabengineforpython3_8'
During handling of the above exception, another exception occurred:
'Traceback (most recent call last):
File "matlab\engine\__init__.py", line 67, in <module>
File "os.py", line 1109, in add_dll_directory
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'F:\\Matlab\\extern\\engines\\python\\..\\..\\..\\bin\\win64'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 17, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "matlab\engine\__init__.py", line 70, in <module>
OSError: Please reinstall MATLAB Engine for Python or contact MathWorks Technical Support for assistance: [WinError 3] The system cannot find the path specified: 'F:\\Matlab\\extern\\engines\\python\\..\\..\\..\\bin\\win64'
[10656] Failed to execute script 'main' due to unhandled exception!
It seems that it can't find the module "matlabengineforpython3_8", which in theory is in the engine folder in the matlab installation path. Then it's looking for a file in the wrong place, because the machine doesn't have an F: disk, but my computer does.
  1 个评论
Christopher Wunder
This is because in matlab\engine\__init__.py it looks for the matlabengineforpython3_8 in the directories specified in _arch.txt (this file should be in the matlab\engine directory) and there are several directories from your computer hardcoded probably from when you installed matlab engine. You could try to manipulate the init file or arch.txt.
You might want to try packaging the matlab functions and scripts by using the PythonPackage functionality from Matlab Compiler SDK, but then you need this toolbox. The packages can then be installed as normal python packages and you don't call the matlab engine anymore but rather use "python import style". The other machine still needs to have matlab installed or at least matlab runtime.

请先登录,再进行评论。

回答(1 个)

Grace Kepler
Grace Kepler 2024-2-16

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by