问题
I developed a small program using python and wxwidgets. It is a very simple program that uses only a mini frame to display some information when needed, and the rest of the time it shows nothing, only an icon in the taskbar.
When I build the exe using py2exe (single file exe mode, optimized), I get a 6MB size file!
I tried not including some libraries or dll that were not needed but still, I can't see why I get such a big file for just a mini frame and an icon in the taskbar.
Is there any way to reduce the size of the exe generated with py2exe?
here is what I did to reduce a bit myself:
options = {"py2exe":{"excludes" : ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter'],
"dll_excludes": ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
'tk84.dll'],
Thanks.
回答1:
The fact that your program is simple does not mean that it's small. Your program has many dependency through the wxWidget stack and 6 Mb does not look that big with all this in mind.
But, back to the question. To shrink a py2exe generated program, you can do a few obvious things:
Distribute less stuff: it seems you already started this road. Look at everything that is distributed along with your program and eliminate it if it's not needed. DllDepend can tell you why a DLL is distributed with your program (probably because a pyd needs it). For other modules, remove them and try if it still works...
Compress it better: run upx on each of your dll. Compress your final program/archive with 7zip maximum compression level.
回答2:
Most of the space is the Python runtime itself. py2exe doesn't "compile" your program to native x86 instructions, or anything like that. It just bundles Python, your *.pyc files, and any modules your program uses up into a bundle that runs on its own.
You could therefore choose to distribute only your *.pyc files and leave it up to the user to provide their own Python distribution and install any needed modules. This isn't a very popular option on Windows, but it's what usually happens everywhere else.
来源:https://stackoverflow.com/questions/1617868/how-to-reduce-size-of-exe-using-py2exe