带有 Pandas 的 PyInstaller 创建超过 500 MB 的 exe

2022-01-10 00:00:00 python pandas pyinstaller conda anaconda

问题描述

我尝试使用 PyInstaller 3.2.1 创建一个 exe 文件,出于测试目的,我尝试为以下代码制作一个 exe:

I try to create an exe file using PyInstaller 3.2.1, for test purpose I tried to make an exe for following code:

import pandas as pd
print('hello world')

经过相当长的时间(15 分钟以上),我完成了 620 MB 的 dist 文件夹并构建了 - 150 MB.我使用 Python 3.5.2 |Anaconda custom(64 位)在 Windows 上工作.可能值得注意的是,在 dist 文件夹中,mkl 文件负责将近 300 MB.我使用pyinstaller.exe foo.py"运行 pyinstaller.我尝试使用 --exclude-module 排除一些依赖项,但最终还是得到了巨大的文件.我使用 onefile 还是 onedir 没有任何区别.

After considerable amount of time (15mins +) I finished with dist folder as big as 620 MB and build - 150 MB. I work on Windows using Python 3.5.2 |Anaconda custom (64-bit). Might be worth noting that in dist folder mkl files are responsible for almost 300 MB. I run pyinstaller using 'pyinstaller.exe foo.py'. I tried using --exclude-module to exclude some dependencies, still ended up with huge files. Whether I use onefile or onedir doesn't make any difference.

我知道 exe 必须包含一些重要文件,但大小接近 1 GB 是否正常?如有必要,我可以提供警告日志或任何有助于解决问题的内容.

I am aware that exe must contain some important files but is it normal to be as big as almost 1 GB? I can provide warning log if necessary or anything that could be helpful to solve the matter.

附:同时,我的同事从相同的示例脚本创建了一个 exe,最终不到 100 MB,不同之处在于他没有使用 anaconda.会是这样吗?

P.S. In parallel my coworker created an exe from same sample script and ended up with less than 100 MB, difference is he is not using anaconda. Could that be the matter?

任何帮助将不胜感激.


解决方案

PyInstaller 从 conda 包创建大可执行文件,从 pip 包创建小可执行文件.从这个简单的python代码:

PyInstaller create the big executable from the conda packages and the small executable from the pip packages. From this simple python code:

from pandas import DataFrame as df
print('h')

我通过 conda 包获得了 203MB 的可执行文件,通过 pip 包获得了 30MB 的可执行文件.但是 conda 是纯 virtualenv 的一个很好的替代品.我可以使用 conda 和 Jupyter 进行开发,创建一些 mycode.py(我可以将 jupyter notebook 作为 py 文件下载到 myfolder 中).但我的最终解决方案是下一个:如果没有,请安装 Miniconda 并从 Windows 开始菜单打开 Anaconda Prompt;

I obtain the 203MB executable by the conda packages and the 30MB executable by the pip packages. But conda is the nice replacement of the pure virtualenv. I can develop with conda and Jupyter, create some mycode.py (I can download jupyter notebook as py-file in myfolder). But my final solution is next: If you do not have it, install Miniconda and from the Windows Start Menu open Anaconda Prompt;

    cd myfolder
    conda create -n exe python=3
    activate exe
    pip install pandas pyinstaller pypiwin32
    echo hiddenimports = ['pandas._libs.tslibs.timedeltas'] > %CONDA_PREFIX%Libsite-packagesPyInstallerhookshook-pandas.py
    pyinstaller -F mycode.py

在我创建新环境exe"的地方,pypiwin32 需要 pyinstaller 但不会自动安装,hook-pandas.py 需要用 pandas 编译.此外,导入子模块并不能帮助我优化可执行文件的大小.所以我不需要这个东西:

Where I create new environment 'exe', pypiwin32 need for pyinstaller but is not installed automaticaly, hook-pandas.py need for compile with pandas. Also, import submodules do not help me optimize the size of the executable file. So I do not need this thing:

from pandas import DataFrame as df

但我可以使用通常的代码:

but I can just use the usual code:

import pandas as pd

此外,在路径中使用国家字母可能会出现一些错误,因此开发工具的英文用户帐户很好.

Also, some errors are possible along using the national letters in paths, so it is nice the english user account for development tools.

相关文章