Search code examples
pythonpython-2.7numpypy2exesoftware-distribution

Reducing py2exe distribution size, while including some numpy functions


I have a program which is currently dependent on numpy which I have been working on converting with py2exe. My issue is that even with a script such as

from numpy import array
print array(1)

that only uses the function numpy.array, I can't find any way to exclude seemingly unnecessary parts of the numpy package such as numpy.linalg in the distribution that is created by py2exe. This results in the distribution being over 80MB in size, (30MB after being zipped). There is a file in the folder called numpy.linalg._umath_linalg.pyd which is 34MB and another called numpy.linalg.lapack_lite.pyd which is 18MB - do these really need to be there?! The .exe does not run if they are simply deleted.

My question is, how can I reduce the resulting distribution size? I am aware there are alternatives to py2exe and that if I could remove dependency on numpy I wouldn't have this problem, but I would like to stick with both of these.

The following setup script is what I am using, resulting in an 87MB distribution.

from distutils.core import setup
import py2exe, sys
import shutil

sys.argv.append('py2exe') # No need to type in command line.

py2exe_options = {
        # 'excludes': ['numpy.linalg'], # Stopped the resulting exe from running
        'compressed': True, # Saves 5MB, is this at the cost of some speed?
        'optimize': 1 # I don't really understand what this does.
        }

setup(
    windows=[{'script': 'main.pyw'}],
    options={'py2exe': py2exe_options},
    )

shutil.rmtree('build', ignore_errors=True) # Remove the build folder

Solution

  • If anyone has any further suggestions I'd like to here them! But here is what I've done so far.

    I have managed to reduce the size of the distribution from 87MB to 34MB by reinstalling numpy using an 'unoptimized' binary downloaded from here. I believe this is likely to run much slower when doing linear algebra operations, however it works fine for me working with arrays.

    UPDATE
    I have now got my distribution down to 28MB by altering the py2exe options in my setup.py script.

    import distutils.core import setup
    
    py2exe_options = {
            'compressed': True,
            'optimize': 1, # 2 does not work.
            'excludes': ['pydoc', 'doctest', 'pdb', 'inspect', 'pyreadline',
                'locale', 'optparse', 'pickle', 'calendar']
            }
    
    setup(windows=['main.py'], options={'py2exe':py2exe_options})