I have two packages (say, dataread
and datainspector
) that were somehow not detected by PyInstaller. Because of this, the application terminates when the running application reaches the point where it needs to import modules from those packages.
The easiest solution would be to copy dataread
and datainspector
into packaged app. But this will break the intention of packaging a binary version of the application.
I've read about hidded imports and hook, and I think that both can solve the problem, but I am not sure of which one to use.
Any suggestions? PS: both these packages may contain nested directories.
The simpler solution is to use --hidden-import=modulename along with the PyInstaller script. It will add modulename as import statement silently. Hooks are better if you want to specify which import needs what additional modules.
Analysis: Finding the Files Your Program Needs To find out, PyInstaller finds all the import statements in your script. It finds the imported modules and looks in them for import statements, and so on recursively, until it has a complete list of modules your script may use.
Hooks are files that specify additional actions when PyInstaller finds import statements.
If you add a hook-data.py
file which contains a line hiddenimports = ['_proxy', 'utils', 'defs']
, PyInstaller will check it to find additional imports when it sees import data
.
You have to specify the path to the hook directory via --additional-hooks-dir
(this is useful if you don't want to mix your source code with compile-only files, and instead keep them in a separate directory).
The simpler solution is to use --hidden-import=modulename
along with the PyInstaller script. It will add modulename
as import statement silently.
Hooks are better if you want to specify which import needs what additional modules. --hidden-import
is simpler as a one-shot or for debugging.
More info - https://pyinstaller.readthedocs.io/en/stable/hooks.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With