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?

- 2,642
- 7
- 13
- 35

- 1,491
- 5
- 22
- 43
3 Answers
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

- 261
- 3
- 4
-
1I 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 Dec 27 '19 at 03:34
-
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 Dec 29 '19 at 23:11
-
2I found a solution for my problem: https://stackoverflow.com/questions/59524507/how-to-make-pyinstaller-not-use-anaconda-and-build-a-small-size-exe-file – mah65 Dec 31 '19 at 01:28
-
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. – Ali Sajjad May 13 '20 at 09:15
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.

- 1,194
- 17
- 24
-
2I said that I have only one line of code `input()` and it took almost 5mb of space. So i dont have any modules loaded. – Jakub Bláha Jun 22 '17 at 05:33
-
It depends friend. It can't be controlled. Try creating exe with py2exe. – Dragon Jun 22 '17 at 05:35
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 (like pandas etc.), 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.

- 7,102
- 69
- 48
- 77

- 368
- 2
- 5
- 19
-
`--onefile` and `--onedir` do _different things_ (see https://pyinstaller.readthedocs.io/en/stable/usage.html#what-to-generate). The executable file sizes are not comparable, because `--onedir` does not bundle required libraries into the executable but places them alongside. – David Reed Sep 05 '21 at 02:29
-
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. – Paweł Pedryc Sep 06 '21 at 01:26