In this Pyinstaller tutorial we will teach you a simple trick on how to reduce the size of your Pyinstaller EXE using the “UPX Packer”.
UPX is a free, secure, portable, extendable, high-performance executable packer for several executable formats. It takes an Executable file as an input, compresses it and produces a new compressed Exe with an average compression ratio of about 50%.
Using UPX with Pyinstaller
The first thing we need to do is go and download the UPX Zip file from their website. (Don’t worry, you don’t have to install anything. Just download the file)
UPX Homepage: https://upx.github.io/
Click on the button which will take you to the latest release download page, or click on a specific version that you want.
The download page looks something like this:
The one you need to download depends on your Operating system version and configuration. You can also just download the source code and compile it yourself.
Windows users should go for the win32 (32 bit) or win64 (64 bit) options located near the bottom. Linux users will probably want to be using one of the first two options.
Save and extract this file anywhere on your device. The folder looks something like this for me (after extraction).
All you need to do now is add an extra option when compiling Pyinstaller to your UPX folder. You can also choose to add the UPX folder to PATH (environment variables), so that Pyinstaller will detect and use it automatically.
I prefer the manual way though, so I can choose whether to add UPX or not.
Our Pyinstaller command with the UPX option looks something like this:
pyinstaller --windowed --upx-dir "C:/Users/CodersLegacy/Dependencies/upx-4.0.1" graphingapp.py
And that’s it! We are done.
All you need to do now is wait for your compilation to complete, and observe the reduction in size. From our side, we achieved a roughly 30% improvement, which is pretty significant. The compression ratio can vary based on various factors, so chances are that you can get an even higher improvement.
Let us know how much of an improvement you got in the comments section below!
If you use Auto-py-to-EXE, you can find the UPX-dir option under the Advanced section.
This marks the end of the “How to use UPX with Pyinstaller” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.