如何使用 cx_freeze 制作可执行文件以及如何减小生成的可执行文件的大小

How to make executables with cx_freeze and how reduce size of the generated executable

我制作了一个 python 脚本,现在我想使用 cx_freeze 使其可执行。我是第一次。

我的代码:

    from cx_Freeze import setup, Executable


build_exe_options = {'packages': ['pandas','os']}


setup(
    name = "monprog",
    version = "0.1",
    options = {"build_exe": build_exe_options},
    description = "Convert_program",
    executables = [Executable("test.py")],
)

它可以工作,但我的文件太大 (2GB)。大量库文件(150 - 200 个库)被导入到可执行助手中。

我的程序很简单,只有 2 个导入(pandas 和 os)和 50 行代码。 "options" 参数可能有错误?

如何减小程序大小?

谢谢`

PS:我用的是anaconda平台

逐字摘自

Part of the purpose of cx_Freeze is to bundle all the dependencies, so that the end user need not install them. numpy, matplotlib, etc. are not small dependencies, especially because they have large binary (non-Python) components. So what you are asking is not physically possible, not because of cx_Freeze limitations, but because of a contradiction between the following two requirements:

  • end user should be able to run executable on essentially any Windows machine, including possibly one without Python, numpy, and matplotlib
  • distributed binary size should be small

You cannot satisfy both these requirements simultaneously. cx_Freeze is appropriate for the first, but not the second. If you can assume your end users already have Python, numpy, matplotlib installed (or can install seperately), and want to distribute your program such that the binary size be small, a wheel is more appropriate.

我成功了。

我用最少的包创建了新的 anaconda 环境(只有 pandas 和 cx_freeze)。然后我用cx_freeze编译。

以前我的文件夹是2go,现在只有280mo。

然而,为了在另一台机器上导出,我必须在我的文件夹中添加 mkl*.dll。这些 dll 位于 anaconda 文件中 在我的电脑上:D:\anaconda\pkgs\mkl-2020.0-166\Library\bin

复制所有 mkl*.dll 和 libiomp5md.dll.

为避免导入错误:mkl_intel_thread.dll