使用pyinstaller和Matlab引擎创建.exe文件



我使用pyqt制作了一个应用程序。从IDE运行时,.py文件运行良好。但是,当使用运行.exe文件时

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

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

Failed to execure script main

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

pyinstaller main.py

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

ImportError: No module named 'mlarray'

我去了PythonPython36Libsite-packagesmatlab中安装的python的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

最后我换了PythonPython36Libsite-packagesmatlab_internalmlarray_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-packagesmatlabengine__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

这变得非常令人沮丧,我不知道该做什么。如果你有什么见解,请告诉我,如果你能帮助我。

首先删除对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进行构建。

相关内容

  • 没有找到相关文章