Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some general tips to reduce the file size for a Pyinstaller generated executable

I am using Pyinstaller to convert a python script to an executable, the script contains multiple imports from various packages like nltk,begin,json and four more. The resulting executable file for a 10 line code is 54MB which is unrealistically high, I know it packages all modules in a single file , but does it make it so large as the python script itself is only 2KB.

What are some general tips to reduce the ".exe" file size?

I am using the command below to convert the script

>pyinstaller.py  -F  -o output check.py

I have only imported individual functions and not entire modules, like:

from nltk.metrics.agreement import AnnotationTask

The imported modules have independent functions and I have selectively imported only 3-4 functions , but this does not seem to have any effect on the generated exe file size, do all functions wastefully get imported in the exe file? How to avoid this?

like image 571
stackit Avatar asked Aug 31 '13 13:08

stackit


1 Answers

You are comparing two totally different things, the exe file you generate does not only contain your file but also the Python interpreter and all imported modules. Looking at my python 2.7 install under windows I see that it is 83 MB large. ntlk and co probably aren't the smallest libraries either and you have to include all of them in the executable one way or the other. For comparison a medium-smallish project of mine generates about 23 MB using cx_freeze (and that includes 10 MB of QT4 dlls), so you could try that.

Also you can compress the executable using UPX, as described here which may help.

like image 87
Voo Avatar answered Sep 22 '22 06:09

Voo