首页 > 解决方案 > 使用 pyinstaller 和 Matlab 引擎创建 .exe 文件

问题描述

我已经使用pyqt. 从 IDE 运行时,.py 文件运行良好。但是,当使用以下命令运行 .exe 文件时:

pyinstaller --name="Myapp" --windowed --onefile main.py

我得到一个 .exe 文件。当我尝试运行它时,它说

Failed to execure script main

我又试了一次,但这次是:

pyinstaller main.py

它确实生成了一个名为mainin的文件夹dist。我main.exe在命令行中运行,我得到:

ImportError: No module named 'mlarray'

我去了matlab我安装的python库\Python\Python36\Lib\site-packages\matlab并更改mlarray.py如下:

from _internal.mlarray_sequence import _MLArrayMetaClass

至:

from matlab._internal.mlarray_sequence import _MLArrayMetaClass

我什至改变了__init__.py:从:

from mlarray import double, single, uint8, int8, uint16, \
    int16, uint32, int32, uint64, int64, logical
from mlexceptions import ShapeError as ShapeError
from mlexceptions import SizeError as SizeError

至:

from matlab.mlarray import double, single, uint8, int8, uint16, \
    int16, uint32, int32, uint64, int64, logical
from matlab.mlexceptions import ShapeError as ShapeError
from matlab.mlexceptions import SizeError as SizeError

最后我改变Python\Python36\Lib\site-packages\matlab\_internal\mlarray_sequence.py 了:

from _internal.mlarray_utils import _get_strides, _get_size, \
    _normalize_size, _get_mlsize, _reshape

至:

from matlab._internal.mlarray_utils import _get_strides, _get_size, \
    _normalize_size, _get_mlsize, _reshape

保存所有工作并尝试再次执行 pyinstaller。

pyinstaller main.py

这次我收到以下错误:

  File "site-packages\matlab\engine\__init__.py", line 62, in <module>
OSError: Please reinstall MATLAB Engine for Python or contact MathWorks Technical Support for assistance: [Errno 2] No such file or directory: 'C:\\pmafgx_das\\das2\\main\\ReleaserQt\\dist\\main\\matlab\\engine\\_arch.txt'
[2028] Failed to execute script main

它变得非常令人沮丧,我不知道该怎么做。请如果你有一个见解,如果你能帮助我,我会appritiate。

标签: d3.jspython-3.xmatlabpyinstallermatlab-engine

解决方案


首先删除对matlab.

然后创建一个目录结构 - 和hook-matlab.py文件 - 如下树:

- script_to_compile.py  # This is the python file you run pyinstaller on
- hooks  # DIR
  - hook-matlab.py

在里面hook-matlab.py

from PyInstaller.utils.hooks import collect_all

datas, binaries, hiddenimports = collect_all('matlab')

然后使用额外的选项构建--additional-hooks-dir=hooks


推荐阅读