Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...