Compile Python script to executable with small output size

Perhaps my question has already been answered somewhere, but I still cannot find a direct answer to it. I want to create a standalone executable from Python code. I have already tried several solutions like py2exe

, pyinstaller

etc. But my problem is the large size of the output file. For example, in a pyinstaller

simple hello world it compiles to a 5MB file. After a lot of searching, I found Cython

which creates a c file from Python code, then I tried to compile it with MSVC 2015

and generated an exe file, but it depends on the file python3x.dll

.

So is there a way to compile Python code for a small standalone EXE file?

If not, how to link the executable with msvc

?

I am using python3.4, but is there any difference between python2 and 3 this way? I use these commands to first generate the c file and then compile it to exe:

python -m cython test.py --embed
cl  /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\Python34\include -Ic:\Python34\PC /Tctest.c /link /OUT:"test.exe" /SUBSYSTEM:CONSOLE /MACHINE:X64 /LIBPATH:c:\Python34\libs /LIBPATH:c:\Python34\PCbuild

      

+4


source to share


1 answer


As far as I know, the relatively large size of the executables causes pyinstaller to reference ALL dependencies from the same library. Therefore, if you are using large libraries like. Matplotlib you will get executables that are about 150MB in size. At least that's my point of view after working with pyinstaller for about two months. One thing I haven't tried yet is playing with the "exclude" option. I read somewhere that it is possible to reduce the file size if you exclude certain modules. Unfortunately I cannot find the source where I read it anymore.

EDIT:



I know it has been a while, but I was able to reduce the size of the executable by excluding some files. This way I was able to shrink the 208MB executable to 70.3MB. I just followed the example here

Python: excluding Pyinstaller modules

+5


source







All Articles