pyi_hooksample/
__init__.py - Hook sample package¶
This is a trivial package, which contains a hidden import and depends on a data file. For these reasons, this library needs PyInstaller hooks in order to be frozen properly by PyInstaller.
import pkgutil
import importlib
__version__ = "0.1"
__all__ = ["do_import", "print_message"]
do_import¶
Uses importlib
to import the _hidden.py - A hidden import module. Since
PyInstaller only performs static analysis, it cannot detect this
import.
def do_import():
Direct use of __import__()
is discouraged in favor of
importlib.import_module()
.
importlib.import_module('._hidden', __name__)
print_message¶
Relies on a data file to work correctly. PyInstaller must be instructed to include this file when freezing for the program to run correctly.
def print_message():
msg = pkgutil.get_data(__name__, 'message.txt').decode().rstrip()
print("Message from ", __name__, ": ", msg, sep="")