Reduce pyinstaller executable size

12,173

Solution 1

The .exe file you create using pyinstaller includes the python interpreter and all modules included in your script.Maybe, the modules you are using have a big library themselves. You can however try using py2exe but it might not work for all projects.The other way to get it smaller is to use a compression program as like, compress the executable using UPX (have a look at this:http://htmlpreview.github.io/?https://github.com/pyinstaller/pyinstaller/blob/v2.0/doc/Manual.html#a-note-on-using-upx). You can also try excluding some items too but at the discretion that removing such items doesn't interfere with the functionality of your .exe.

Solution 2

Ah, You are not creating the build in a separate virtual environment.

Create a virtual environment just for build purpose and install the packages you need in this environment.

in your cmd execute these to create a virtual enviornment

python -m venv build_env

cd build_env

C:\build_env\Scripts\Activate

you will see this >>(build_env) C:\build_env

Install all the packages you need for your script, start with pyinstaller

pip install pyinstaller

Once you are all installed, build the exe as before. The exe built using the virtual environment will be faster and smaller in size!! For more details check https://python-forum.io/Thread-pyinstaller-exe-size

Share:
12,173
Jakub Bláha
Author by

Jakub Bláha

Updated on June 11, 2022

Comments

  • Jakub Bláha
    Jakub Bláha 6 months

    I have only one line of code input() written in python and packed with pyinstaller with option --onefile. The exe file is 4577 kB which is almost 5Mb. How can I reduce its size or exclude some auto-bundled libraries?

  • Jakub Bláha
    Jakub Bláha over 5 years
    I said that I have only one line of code input() and it took almost 5mb of space. So i dont have any modules loaded.
  • Dragon
    Dragon over 5 years
    It depends friend. It can't be controlled. Try creating exe with py2exe.
  • mah65 almost 3 years
    I followed your method. Without any package, the file was only 6 MB, and did not work, because I need pandas. Then I did pip install pandas. The size reached 212 MB! What should I do?! :(
  • mah65 almost 3 years
    I found that the pyinstaller in the build_env also uses pyinstaller and python in other env for building!!! This is why the size does not change much!
  • mah65 almost 3 years
    I found a solution for my problem: stackoverflow.com/questions/59524507/…
  • Ali Sajjad over 2 years
    Running "pip install pyinstaller" will use our global environment. We need to switch to the new creates environment by running the "activate" located at 'C:\build_env\Scripts\activate'.... Go to scripts folder and run "activate" on cmd.
  • David Reed
    David Reed over 1 year
    --onefile and --onedir do different things (see pyinstaller.readthedocs.io/en/stable/…). The executable file sizes are not comparable, because --onedir does not bundle required libraries into the executable but places them alongside.
  • Paweł Pedryc
    Paweł Pedryc over 1 year
    True, but in --onedir case (finally used by me): I've measured the size of a folder, not .exe. The "size problem" is related to the fact that the regular compressor "sucks in" all libraries that were installed (whenever) via the Anaconda terminal. When I used the auto-py-to-exe library, then I could choose --onedir too. The size of the output was - as I said - 911 MB.