Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove/exclude modules and files from pyInstaller?

I'm trying to shrink the size of my python exe file, I've been looking around but I can't seem to find a good answer for removing extra modules. At the moment, I'm discovering that it's deleting modules I need instead of the ones I'm telling it to. The documentation is rather unhelpful and neither are examples I've found so far.

My spec file:

a = Analysis(['D:\\<path>\\<scriptName>.py'],
         pathex=['c:\\bin\\pyinstaller-2.0'],
         hiddenimports=[],
         hookspath=None,
         )

pyz = PYZ(a.pure)
exe = EXE(pyz,
      a.zipfiles,
      a.scripts,
      a.binaries,
      a.datas + [('data/Sounds/Cycle.wav', 'D:\\<path>\\data\\Sounds\\Cycle.wav','DATA'),
       ('data/Sounds/Hold.wav', 'D:\\<path>\\data\\Sounds\\Hold.wav','DATA'),
       ('data/Sounds/Timer.wav', 'D:\\<path>\\data\\Sounds\\Timer.wav','DATA'),
       ('data/Sounds/Warn.wav', 'D:\\<path>\\data\\Sounds\\Warn.wav','DATA'),
       ],
      name=os.path.join('dist', 'timer.exe'),
      debug=False,
      strip=False,
      upx=False,
      icon=r"D:\<path>\Icon.ico",
      console=True )

Now, I want to start excluding things, but there doesn't seem to be a very explanation of how to exclude things.

These are things I had excluded when using py2exe:

'win32', 'unittest', _ssl, 'python25.dll', 'w9xpopen.exe', 'wx'
'python25.dll', 'API*', 'KERNALBASE.dll', 'DEVOBJ.dll','CRMGR32.dll',
'POWERPROF.dll', 'msvcm90.dll', 'msvcp90.dll', 'msvcr90.dll'

Though if I add any of these into the a.binaries as

a.binaries -[('wx')],

it deletes the PyQt4.dll files instead. Same holds true for the others. I do not follow that logic. I would think, at the very least, if it couldn't find them in the first place it would just skip over them instead of deleting other things.

py2exe makes me a 26mb files + three files (exe, library.zip, and w9xpopen.exe) pyInstaller makes me an 11mb file, and one file.

I feel I can make it smaller, but this excludes thing is confusing me. It straight up ignores the msv dll files and puts them in anyway.

Using Python 2.7, PyQt4 4.9.x

like image 661
ooklah Avatar asked Jun 10 '13 23:06

ooklah


3 Answers

I would remove some like this:

a.binaries = a.binaries - TOC([
  ('sqlite3.dll', None, None),
  ('tcl85.dll', None, None),
  ('tk85.dll', None, None),
  ('_sqlite3', None, None),
  ('_ssl', None, None),
  ('_tkinter', None, None)
])
like image 200
Juanmi14 Avatar answered Nov 01 '22 02:11

Juanmi14


The solution that worked for me was:

excluded_binaries = [
        'VCRUNTIME140.dll',
        'msvcp140.dll',
        'mfc140u.dll']
a.binaries = TOC([x for x in a.binaries if x[0] not in excluded_binaries])
like image 40
johnlu Avatar answered Nov 01 '22 03:11

johnlu


An easy way to exclude is to use the Analysis class, and add your modules to the excludes field, like stated here https://pyinstaller.readthedocs.io/en/stable/spec-files.html#spec-file-operation

What I use is something like this:

Analysis(..., excludes=['_gtkagg', '_tkagg', 'bsddb', 'curses', 'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants', 'Tkinter'], ..)
like image 41
hDan Avatar answered Nov 01 '22 03:11

hDan