In this Python Pyinstaller tutorial, we will show you several methods you can use to reduce the size of your Pyinstaller EXE.
How to reduce Pyinstaller EXE size?
There are three different techniques we will cover in today’s tutorial, on how we can reduce the size of your Pyinstaller EXE.
- Using UPX
- Virtual Environments
- Optimizing your Code and Imports
Using UPX
Using the UPX packer is an easy and effective way of reducing the size of your Pyinstaller EXE by about 20 – 50% (varies based on the Code and Libraries used).
All you have to do is go to the UPX website, and download the appropriate folder. No need to install anything, just unzip it anywhere on your device.
Now if you are on windows, you should note down the filepath of the UPX folder, and add it to your Environment Variables (PATH). Pyinstaller will automatically detect and use UPX if you have it added to PATH.
For those of you who do not want (or cannot) to add it to PATH, you can also add it directly into the pyinstaller command using the following option:
--upx-dir="file/path/to/upx/dir"
Note: Do not make the silly mistake of including the EXE in the filepath. You need to include the filepath to the UPX folder, not the EXE inside it.
Virtual Environments
Although UPX is the most easiest way to reduce the size of your Pyinstaller EXE, using a Virtual Environment is actually the most effective, and most recommended.
How does this work exactly?
Well, a Virtual environment is basically like a fresh Python installation, with no libraries installed. Why is this beneficial though?,
The problem that most people face, is that many “extra” or “un-needed” libraries end up inside their EXE’s. For example, when I was creating a “Tkinter” (a GUI library) application, “PyQt6” (another GUI library) was also included, even though I was not using PyQt6 inside my application. (I had PyQt6 installed on my device)
This is pretty common, and happens in Pyinstaller and other similar EXE converting libraries.
To resolve this, we use Virtual Environments and only install the required libraries that we know we will be using. This ensures that Pyinstaller will not “accidentally” include any other libraries (because there will no extra libraries installed in the Virtual environment).
Unfortunately, the explanation required to explain Virtual Environments and how to set it up, is a bit lengthy. Hence, we have a created a separated dedicated tutorial to show you how to do so. We have used a sample Pyinstaller application there, and also shown the “before” and “after” sizes.
Follow this link to go to Pyinstaller + Virtual Environments Article.
Optimizing your Code and Imports
This is a bit of an extension, as to what we discussed in the Virtual Environments section. While Virtual environments will ensure only the libraries that you explicitly import are imported, you should double-check to make sure all of the libraries you are importing are actually needed.
Are you making some imports that you never actually use? Remove them!
You can also try and evaluate whether you can possibly remove some libraries you are using. If you are only using a single function, or single feature from that library, this will cause the whole library to be imported.
You can either:
- Try making a custom implementation of that feature
- Find a smaller library that offers that feature.
- Find an existing library that you are already using, which may offer the same (or similar) feature.
Other than this, you should work on optimizing and reducing redundant code. The smaller your code files are, the smaller your EXE will be. The gains from this will obviously be rather minimal, so don’t expect a drastic difference.
This marks the end of the “Reduce the size of your Pyinstaller EXE” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.