Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 715
swdev Avatar asked Jan 14 '15 16:01

swdev


People also ask

How do I specify hidden imports in PyInstaller?

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.

Does PyInstaller include imports?

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.


1 Answers

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

like image 139
Dawid Gosławski Avatar answered Sep 18 '22 06:09

Dawid Gosławski