如何压缩 python .exe 文件大小
我已使用 this 将 python 文件转换为 exe 但是.exe的大小是800mb,有什么办法可以减少它的大小吗?
I have converted python file to exe using this
But the size of .exe is 800mb, is there any way I can reduce its size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Pyinstaller
pyinstaller 的
--onefile
标志(例如,pyinstaller myprogram.py --onefile
或pyinstaller myprogram.py -F
将所有内容放入一个可执行文件中,包括所有已安装依赖项的所有代码,即使您只使用其中的一个函数。--onedir
或。-D
标志将这些依赖项放在与编译代码一起打包的目录中,它往往会大大减少 .exe 文件的大小但是,我仍然不建议使用。 Pyinstaller。经过广泛的实验,我发现在尚未具备 Python 友好环境的计算机上执行时间非常慢(大部分执行时间缓慢来自于加载这些依赖项。)
使用cx_Freeze
对于我为我的部门构建的可执行文件,我使用 cx_Freeze 构建安装程序或磁盘映像。
为此,您所要做的就是编写安装脚本然后调用
py setup.py bdist_msi
(Windows) 或py setup.py dbist_dmg
(Linux)。生成的安装程序相对较小,并且一旦安装,它将比我见过的任何 Pyinstaller 选项执行得快得多。
Using Pyinstaller
The
--onefile
flag with pyinstaller (e.g.,pyinstaller myprogram.py --onefile
orpyinstaller myprogram.py -F
puts everything in just a single executable, including all the code of all your installed dependencies, even if you're using just one function from them. That's a lot!The
--onedir
or-D
flag instead puts the those dependencies in a directory that's packaged alongside your compiled code. It tends to reduce the size of the .exe file considerably.But still, I wouldn't recommend using Pyinstaller. Having experimented with it extensively, I've found that execution time is incredibly slow on machines that don't already have a Python-friendly environment. (Most of the slow execution time comes from loading those dependencies.)
Using cx_Freeze
For the executables that I build for my department, I use cx_Freeze to build an installer or a disk image.
To do so, all you have to do is write a setup script and then call
py setup.py bdist_msi
(Windows) orpy setup.py dbist_dmg
(Linux).The resulting installer will be relatively small and, once it's installed, it will execute much faster than any of the Pyinstaller options I've seen.