Search code examples
pythonpython-3.xpyqt5cx-freeze

Python3 compiled App Decrease Size with zip?


I have compiled with success a rather big app ,but the size of the app is way too big(about 1 GB).I realized that almost the half (400MB) was taken from just 2 modules(PyQt5 and mpl_toolkits(basemap)).Of course the one approach is to delete the side-services you dont need but i cant in my case as i use about all of these in my app. Then i was thinking about ziping these modules but i don't know if this is safe or execute time viable. So is there a suficient way to minimize the size of my app, and if the ZIP is the only answer is it viable to do this on a lib such PyQt5 witch is imported many times in my script?


Solution

  • It is entirely possible to do this; Python has always supported "zipapps", and as of 3.5, provides a module, zipapp, to assist with creating them.

    The only concern is whether your dependencies run properly zipped; most do, but some take file-system layer dependencies that zipping (and eggs and the like) don't handle (the setup.py file for these would declare zip_safe=False to tell tools not to allow it). Packages with C extensions would have to assert zip_safe=True, as the default analyzer assumes C extensions can't be safely zipped (though many can).

    It will slow down startup a bit (since it has to decompress the modules), but it shouldn't dramatically slow runtime, no matter how many times you import it; the first import is the only one that really matters, since subsequent imports just pull the cached copy of the module from an internal cache (sys.modules).