0
votes

I have developed the UI using the PyQt5, now i am converting the code into standalone application using Pyinstaller, but the problem is the file size is coming huge (nearly 250MB). Can i reduce the size of my file but excluding the unnecessary imports of PyQt5. Following the libraries i have used -

from PyQt5 import QtCore, QtGui, QtWidgets
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import os,sys
from mat4py import loadmat
from matplotlib import pyplot as plt

PyQt5 library has following many sub modules (QtCore, QtGui, QtWidgets, QtMultimedia, QtBluetooth, QtNetwork, QtPositioning, Enginio, QtWebSockets, QtWebKit, QtWebKitWidgets, QtXml, QtSvg, QtSql, QtTest) in which i am using only QtCore, QtGui and QtWidgets.

Similarly, library matplotlib many modules in which i am using pyplot.

I want to skip the modules which the program doesn't using, so that i can reduce the size of the executable file.

My Spec file of pyinstaller -

import sys
sys.setrecursionlimit(3000)

block_cipher = None

a = Analysis(['ReadMAT.py'],
             pathex=['C:\\Users\\Sekhar\\Documents\\PythonScripts\\RXP\\ReadMAT'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='ReadMAT',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False, icon='ReadMAT.ico')

How to skip the submodules which will not get into the compilation process. How can i reduce the file size of the executable ?

1

1 Answers

1
votes

PyInstaller has a hook mechanism for each package and it handles the binaries needed for the packages.

For some well-known libraries like Qt, it implements an efficient hook file that only retrieves the necessary binaries. But if you want to exclude some parts you need to do it by yourself either by using exclude command or manipulating the hook file:

  1. By excluding the unnecessary modules with --exclude-module. Usually, this would be enough.

  2. By modifying the specific hook file and remove the binaries you don't need.

    For example, in Qt's hook file (<Pyinstaller_path>/utils/hooks/qt.py), there is a variable called _qt_dynamic_dependencies_dict and has all the binaries which located in <qt_installation_path>/Qt/bin so you can remove each one you don't need.

    Later in a function called get_qt_binaries which bundles other Qt binaries, you can remove each one you don't need like opengl32sw.dll.

For example:

_qt_dynamic_dependencies_dict = {
    ## "lib_name":              (.hiddenimports,           translations_base,  zero or more plugins...)
    # I've removed qt5bluetooth with commenting below line
    #"qt5bluetooth":             (".QtBluetooth",           None,               ),  # noqa: E241,E202
    "qt5concurrent":            (None,                     "qtbase",           ),
    ...
}
...

def get_qt_binaries(qt_library_info):
    binaries = []
    angle_files = ['libEGL.dll', 'libGLESv2.dll', 'd3dcompiler_??.dll']
    binaries += find_all_or_none(angle_files, 3, qt_library_info)

    # comment the following two lines to exclude the `opengl32sw.dll`
    # opengl_software_renderer = ['opengl32sw.dll']
    # binaries += find_all_or_none(opengl_software_renderer, 1, qt_library_info)

    # Include ICU files, if they exist.
    # See the "Deployment approach" section in ``PyInstaller/utils/hooks/qt.py``.
    icu_files = ['icudt??.dll', 'icuin??.dll', 'icuuc??.dll']
    binaries += find_all_or_none(icu_files, 3, qt_library_info)

    return binaries