python - 使用 pyinstaller 和 numpy 的可执行文件的大小

标签 python numpy pyinstaller

我使用 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 的同时减小大小?也许只包含我真正使用的 dll

最佳答案

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

Install numpy with: 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。

关于python - 使用 pyinstaller 和 numpy 的可执行文件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47769904/

相关文章:

python - 简明地说 "none of the elements of an array are None"?

python - 为什么第一个卷积层权重在训练过程中不改变?

python - 值错误 : could not convert string to float, NumPy

python - 使用 pyinstaller 编译 celery worker

python - 如何在 Windows 上使用 PyInstaller 创建 Linux 和 MacOS 可执行文件?

python - 在子字符串的另一列中过滤和移动文本

python - 如何在 django 查询集中 AND 链接过滤器?

python - pyinstaller错误: OSError: [WinError 6] The handle is invalid

python - 使用 Matplotlib 绘制数据子集

python - bin 一列并对 (2,N) 数组的另一列求和