【问题标题】:size of executable using pyinstaller and numpy使用 pyinstaller 和 numpy 的可执行文件大小
【发布时间】:2018-05-25 23:28:38
【问题描述】:

我使用 numpy 和 Qt 编写了一个短程序 main.py

from PyQt5 import QtWidgets
import numpy as np
import sys

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QTextEdit('hello')
    w.show()
    sys.exit(app.exec_())

当我使用 pyinstaller --windowed main.py 时,生成的 dist 文件夹大小为 390MB。

如果我删除 import numpy as np,生成的 dist 文件夹只有 70MB。

有什么方法可以在使用 numpy 的同时减小大小?也许只包括我真正使用的dlls

【问题讨论】:

  • 基本上,即使是 numpy 中最简单的功能也基于一些 BLAS 库(还有一些更复杂的:LAPACK)。这些都是巨大的,有些(MKL)甚至比其他的(OpenBLAS)更大。
  • 所以没有机会减小可执行文件的大小并保持其独立性?
  • 我也有同样的问题,你找到解决方法了吗? - 除了在代码库中完全排除 Numpy...
  • link。事实证明,可执行文件的大小主要取决于用于 numpy 的库的类型,正如 sachsa 所解释的那样。有些人通过确保使用 OpenBLAS 而不是例如使用 OpenBLAS 来获得

标签: python numpy pyinstaller


【解决方案1】:

感谢用户 balletpiraat 我找到了解决方案。

使用以下命令安装 numpy: conda install -c conda-forge numpy

为了测试这一点,我创建了两个 anaconda 环境。环境“normalnumpy”是用以下方式创建的:

conda create -n normalnumpy python=3.7
activate normalnumpy
conda install numpy
pip install pyinstaller

导致:

altgraph                  0.16.1                    <pip>
blas                      1.0                         mkl
certifi                   2018.10.15               py37_0
future                    0.16.0                    <pip>
icc_rt                    2017.0.4             h97af966_0
intel-openmp              2019.0                      118
macholib                  1.11                      <pip>
mkl                       2019.0                      118
mkl_fft                   1.0.6            py37hdbbee80_0
mkl_random                1.0.1            py37h77b88f5_1
numpy                     1.15.2           py37ha559c80_0
numpy-base                1.15.2           py37h8128ebf_0
pefile                    2018.8.8                  <pip>
pip                       10.0.1                   py37_0
PyInstaller               3.4                       <pip>
python                    3.7.0                hea74fb7_0
pywin32-ctypes            0.2.0                     <pip>
setuptools                40.4.3                   py37_0
vc                        14.1                 h0510ff6_4
vs2015_runtime            14.15.26706          h3a45250_0
wheel                     0.32.1                   py37_0
wincertstore              0.2                      py37_0

环境“extranumpy”是通过以下方式创建的:

conda create -n extranumpy python=3.7
activate extranumpy
conda install -c conda-forge numpy
pip install pyinstaller

导致:

altgraph                  0.16.1                    <pip>
blas                      1.1                    openblas    conda-forge
certifi                   2018.10.15            py37_1000    conda-forge
future                    0.16.0                    <pip>
libflang                  5.0.0             vc14_20180208  [vc14]  conda-forge
llvm-meta                 5.0.0                         0    conda-forge
macholib                  1.11                      <pip>
numpy                     1.15.2          py37_blas_openblash8d851b4_1  [blas_openblas]  conda-forge
openblas                  0.2.20                   vc14_8  [vc14]  conda-forge
openmp                    5.0.0                    vc14_1  [vc14]  conda-forge
pefile                    2018.8.8                  <pip>
pip                       10.0.1                   py37_0
PyInstaller               3.4                       <pip>
python                    3.7.0                hea74fb7_0
pywin32-ctypes            0.2.0                     <pip>
setuptools                40.4.3                   py37_0
vc                        14                            0    conda-forge
vs2015_runtime            14.15.26706          h3a45250_0
wheel                     0.32.1                   py37_0
wincertstore              0.2                      py37_0

我用“main.py”测试了这两个环境:

import numpy as np
if __name__ == '__main__':
    test = np.array([42])
    print(test)

pyinstaller --onefile main.py

“normalnumpy”可执行文件为 228MB,“extranumpy”可执行文件为 10MB。

【讨论】:

  • exe文件会像平常一样存放在Dist目录下吗?
  • 我已经创建了(基本)环境并且有点卡住了。我怎么出去?
  • @Miffy 有many tutorials如何创建环境。如果你这样做了,你必须激活它并像往常一样运行 pyinstaller。这些文件将位于dist 文件夹中,就像从“正常”python 安装中运行它一样。所有命令和代码都发布在我的答案中。我建议您尝试该示例并从那里继续。
  • @Miffy 你应该问一个单独的问题,如果没有错误消息和 cmets 很难解决。
  • 我必须添加约束blas=*=openblas 以防止安装mkl。找到这个推荐here
【解决方案2】:

您是否尝试过排除模块?这是一个常见的问题。你也可以看看:

Reducing size of pyinstaller exe

值得一提,您使用的是 Anaconda 吗?一个全新的 oracle box python 系统,并且没有一个大文件。

我相信您可以在某个地方的文档中调整找到目录的位置。

https://pythonhosted.org/PyInstaller/spec-files.html

【讨论】:

  • 如果我排除模块,可执行文件不再是独立的,我可以排除我不使用的部分,例如 numpy 吗?
  • @Jonas 您可以确定排除模块,但不能排除部分 numpy。也许理论上你可以……不,那会破坏 numpy。根据我链接的帖子,UPX 和虚拟环境以及排除是减小大小的一般方法。
猜你喜欢
  • 1970-01-01
  • 2018-07-15
  • 2017-11-18
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
  • 2013-08-27
  • 1970-01-01
相关资源
最近更新 更多