Python: Excluding Modules Pyinstaller

pyinstaller - Excluding Modules

Once you have activated you virtual environment make sure you pip install pyinstaller. Otherwise you will be using the global pyinstaller which has access to all global modules.

By installing pyinstaller into your virtual env, when you run the command it will only have access to the modules inside the virtual env.

If you still continue to see modules after that, then those are the modules pyinstaller deemed necessary to run the executable.

PyInstaller: cannot exclude a module

My suggestion is that you use a virtual env containing only the required modules. It is a better development practice.

Also, you can use --exclude-module flag to list all the modules you want to exclude. (Pyinstaller automatically includes unneeded modules)

Pyinstaller: trying to exclude all modules in subfolder, but pyinstaller keeps compiling them in anyway

Ultimately, what I had to do to get it working was:

  1. Move the entire data folder to a separate project, stopping pyinstaller from picking it up. Pyinstaller only has access to main.py and gameobj.py.

  2. Change how main.py looks for imports:

import sys
sys.path.append(".\data")
sys.path.append(".\data\powers")
sys.path.append(".\data\effects")
sys.path.append(".\data\spells")
sys.path.append(".\data\ais")

from gameobj import *
import data.gamedata as gamedata

May not all be strictly necessary. Everything else is as it was previously.


  1. gamedata.py is slightly modified to the following:
from data.powers import lore
from data.effects import haunt
from data.spells import bottomlesspit
from data.ais import dummy

  1. Run pyinstaller with following command:
    pyinstaller -n Game --exclude-module gamedata --noconfirm main.py

  2. After running pyinstaller, copy the data folder into dist\Game and run Game.exe. Though gamedata.py isn't frozen in with main.py and gameobj.py, the application will still run through the tests that use things from the data folder, and will pick up changes made to those files.

Pyinstaller excluding SubModules while Compiling

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

Pyinstaller automatically includes unneeded modules

Not sure if this really counts as a solutions. But by ignoring winpython all together and using a standard installation of python that only had pip installs of pyinstaller and pandas added to it I was easily able generate a functional .exe that was 18MB. I guess it had something to do with winpython.

Getting unrecognized arguments error when trying to exclude modules in PyInstaller

You need to split up the flags and its argument.

For Example:

PyInstaller.__main__.run([
'res/output/output.py',
'--onedir',
'--exclude-module',
'PIL',
'--noconsole'
])


Related Topics



Leave a reply



Submit