简体   繁体   English

如何减少 cx_Freeze 编译后的 Python 可执行文件大小?

[英]How to reduce cx_Freeze compiled Python executable size?

I've compiled a Python program with cx_Freeze .我用cx_Freeze编译了一个 Python 程序。 The Python code itself is only 195 lines long. Python 代码本身只有 195 行。 However, my compiled program is 500 MB .但是,我编译的程序是500 MB I use matplotlib , numpy , and pymsgbox and I don't know if my libraries affect my compiled program size that much.我使用matplotlibnumpypymsgbox ,我不知道我的库是否对我编译的程序大小影响那么大。

What is the reason for this unexpected file size and is there a way to reduce the size of my executable?这种意外文件大小的原因是什么,有没有办法减少我的可执行文件的大小?

Part of the purpose of cx_Freeze is to bundle all the dependencies, so that the end user need not install them. cx_Freeze 的部分目的是捆绑所有依赖项,以便最终用户无需安装它们。 numpy , matplotlib , etc. are not small dependencies, especially because they have large binary (non-Python) components. numpymatplotlib等不是小依赖,特别是因为它们有很大的二进制(非 Python)组件。 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:所以你所问的在物理上是不可能的,不是因为 cx_Freeze 的限制,而是因为以下两个要求之间的矛盾:

  • end user should be able to run executable on essentially any Windows machine, including possibly one without Python, numpy, and matplotlib最终用户应该能够在基本上任何 Windows 机器上运行可执行文件,包括可能没有 Python、numpy 和 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. cx_Freeze 适用于第一个,但不适用于第二个。 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.如果您可以假设您的最终用户已经安装了 Python、numpy、matplotlib(或可以单独安装),并且希望分发您的程序以减小二进制文件的大小,那么轮子更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM