Reducing size of pyinstaller exe

PythonPandasSeleniumSelenium WebdriverPyinstaller

Python Problem Overview


I have a simple pandas pyinstaller exe which is over 40MB.

My exe example:

import collections
import csv
import selenium
import pandas

print('hi')

40MB+ for this seems a bit overkill.

How can I reduce this as much as possible?

One method:

pyinstaller --onefile --exclude matplotlib --exclude scipy --exclude pandas --exclude numpy.py

This however is not practical considering how big the exclusion list would be.

How do I select a folder for pyinstaller to get modules from and exclude everything else so I may have a small application?

Spec file:

a = Analysis(['123.py'],
             pathex=['C:\\Users\\AA\\ZZ'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='123',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

It's also worth mentioning. By default, Pyinstaller does not detect pandas.

Add:

hiddenimports = ['pandas._libs.tslibs.timedeltas']

To: C:\Users\<NAME>\AppData\Local\Programs\Python\Python36\Lib\site-packages\PyInstaller\hooks

A possible solution when using multiple executables, could be to link each executable to a separate folder or executable with all imports.

Python Solutions


Solution 1 - Python

try setting up your environment with a virtualenv, and install in there only the required libraries

some details on working with virtual env are here: https://virtualenv.pypa.io/en/stable/

Solution 2 - Python

For me, it is a simple case of using pandas that the exe is huge.

Though removing certain directories was helpful, as was UPXING that helped a great deal also.

I got it reduced a lot and it was not doing this by default.

That being said, the final and most import solution is talked about here: https://stackoverflow.com/questions/47610050/importing-python-modules-from-a-select-location . So there was a feature that did all this, but for now there is some manual handling involved because: multipackage-bundles is broken.

Now to the simple solution for lots of exe's

If you have many executables, I highly recommend this approach:

pyinstaller -F abc.py --onedir (Have all imports of both scripts)
pyinstaller -F abd.py --onedir (Have all imports of both scripts)

Now put abd.exe in the one directory of abc.py folder as well as any other external scripts. Be sure they are differently named or only one script will run.

This works really well because all the dependencies are in one folder. This is how it should be. So in this example say you had a 40mb one folder. For each additional exe afterwards, it will only be +5mb(or how big the exe is) rather than 40mb each.

Solution 3 - Python

The python interpreter and all imported modules are included in the executable.

You can try adding modules you want to exclude to the excludes list under Analysis in your spec file.

You could also try compressing the executable using UPX. See https://htmlpreview.github.io/?https://github.com/pyinstaller/pyinstaller/blob/v2.0/doc/Manual.html#a-note-on-using-upx">A note on using UPX

Solution 4 - Python

I use the Anaconda environment and so the virtualenv solution isn't an option. my way was to exclude unnecessary modules in my spec file, ex.:

in Analysis(...)

excludes=['pandas', 'numpy'],

(this are modules whose raise the size of the files extraordinarily)

For every build i'm using this adjusted spec file to create the exe.

pyinstaller "mySpec.spec" --distpath="<path>"

Solution 5 - Python

I had a similar problem and found a solution. I used Windows terminal preview. This program allows creation of various virtual environments like Windows Power Shell (btw. Linux Ubuntu too. Also, worth noting: you can have many terminals in this program installed and, even, open a few at once. Very cool stuff).

Inside Windows Power Shell in Windows terminal preview I installed all the necessary libraries, then I opened the path to my file and tried to use this command:

pyinstaller --onefile -w 'filename.py'

...but, the output exe didn't work. For some reason, the console said that there is a lack of one library (which I had installed earlier). I've found the solution in mimic the auto-py-to-exe library. The command used by this GUI is:

pyinstaller --noconfirm --onedir --console "C:/Users/something/filename.py"

And this one works well. I reduced the size of my output exe program from 911MB to 82,9MB !!!

BTW. 911MB was the size of output made by auto-py-to-exe.

I wonder how is it possible that no one yet has created a compressor that reads the code, checks what libraries are part of the code, then putting only them inside the compression. In my case, auto-py-to-exe probably loaded all libraries that I ever installed. That would explain the size of this compressed folder.

Some suggest using https://virtualenv.pypa.io/en/stable/ but in my opinion, this library is very difficult, at least for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser9062171View Question on Stackoverflow
Solution 1 - PythonOphir YoktanView Answer on Stackoverflow
Solution 2 - Pythonuser9062171View Answer on Stackoverflow
Solution 3 - Pythonuser2556381View Answer on Stackoverflow
Solution 4 - PythondroebiView Answer on Stackoverflow
Solution 5 - PythonPaweł PedrycView Answer on Stackoverflow