8

My question might already have been answered somewhere but still, I can't find a straightforward answer to it yet. I want to create a standalone executable from python code. I've already tried a bunch of solutions like py2exe, pyinstaller, etc. But my problem is the large output file size. For example, in pyinstaller a simple hello world compile to 5MB file. After a lot of searches I've found Cython which creates a c file from python code then I tried to compile it with MSVC 2015 and generated the exe file but it depends on python3x.dll file.

So Is there any way to compile the python code to stand alone exe file with a small size?

If it is not, how to bundle the executable file with msvc?

I'm using python3.4 but is there any difference between python2 and 3 in this manner? I am using these commands to first generate c file 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
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • Every packaging method I've seen still requires you to have the Python3x.dll available. The only thing I've seen people do is put it in a zip file, but never totally in the EXE – Matt Jul 25 '17 at 17:11
  • Thanks for the answer,So you mean If I compress the exe manually is my best bet? – Masoud Rahimi Jul 25 '17 at 19:08
  • What I mean is that the EXE is going to sit separate from your libraries which at best you can put in a zip file together to make the package smaller. – Matt Jul 28 '17 at 20:03

1 Answers1

7

As far as I know the relatively big size of the executables results in the fact, that pyinstaller links to ALL dependencies of one library. So if you use big libraries as e.g. Matplotlib you will get executables with the size of around 150MB. At least that is my point of view after working with pyinstaller for about two month. One thing I have not tried yet is to play around with the "exclude" option. I have read somewhere that it is possible to reduce the file's size if you exclude specific modules. Unfortunately I can not find the source where I read it anymore.

EDIT:

I know it's been a while but I managed to reduce the size of the executable by excluding some files. That way I was able to reduce an executable with a size of 208MB to a size of 70.3MB. I Just followed the example here

Python: Excluding Modules Pyinstaller

stranger0612
  • 301
  • 1
  • 2
  • 12