Python – How to properly create a pyinstaller hook, or maybe hidden import


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.

Best Solution

Hooks are files that specify additional actions when PyInstaller finds import statements.

If you add a 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 -

Related Question