Reduce pyinstaller executable size
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

Jakub Bláha
Updated on June 11, 2022Comments
-
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 over 5 yearsI 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 over 5 yearsIt depends friend. It can't be controlled. Try creating exe with py2exe.
-
mah65 almost 3 yearsI 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 yearsI 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 yearsI found a solution for my problem: stackoverflow.com/questions/59524507/…
-
Ali Sajjad over 2 yearsRunning "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 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 over 1 yearTrue, 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 theauto-py-to-exe
library, then I could choose--onedir
too. The size of the output was - as I said - 911 MB.