Pyinstaller - pack into onefile as a second step after generating multi-file dist to reduce size

Viewed 131

I'm using Pyinstaller to generate an executable of a Python3 application that includes a UI made with PySide2.

Using using the --onefile flag the resulting .exe is huge,140 MB.

I tried generating without --onefile flag and take a look what was included in the dist folder: to my surprise everything from PySide2 was included, hence the size.

Keeping in mind that I imported only a few thing from PySide2 (namely: QDialog, QLabel, QPushButton, QApplication, QVBoxLayout) I proceded to manually delete clearly unnecessary files (i.e. QtBluetooth, QtCharts...) and the main executable still works perfectly.

Is there a way to either:

a. proceed to generate the single file executable after the manual removal of unneccessary files

or

b. specify exactly what from PySide should be packed? The import in the Python script looks like from PySide2.QtWidgets import QDialog, QLabel, QPushButton, QApplication, QVBoxLayout but everything is being packed instead.

1 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 should be smaller in size!!

Related