Python 如何正确创建 pyinstaller 钩子,或者隐藏导入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27947639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly create a pyinstaller hook, or maybe hidden import?
提问by swdev
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.
我有两个包(比如dataread
和datainspector
),它们以某种方式没有被 PyInstaller 检测到。因此,当正在运行的应用程序到达需要从这些包中导入模块时,应用程序就会终止。
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.
最简单的解决方案是复制dataread
并datainspector
放入打包的应用程序中。但这会破坏打包应用程序二进制版本的意图。
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.
有什么建议?PS:这两个包都可能包含嵌套目录。
回答by Dawid Gos?awski
Hooks are files that specify additional actions when PyInstaller finds import statements.
挂钩是在 PyInstaller 找到导入语句时指定附加操作的文件。
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
.
如果您添加一个hook-data.py
包含一行的文件hiddenimports = ['_proxy', 'utils', 'defs']
,PyInstaller 将在它看到时检查它以查找其他导入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).
您必须通过指定钩子目录的路径--additional-hooks-dir
(如果您不想将源代码与仅编译文件混合在一起,而是将它们保存在单独的目录中,这将很有用)。
The simpler solution is to use --hidden-import=modulename
along with the PyInstaller script. It will add modulename
as import statement silently.
更简单的解决方案是--hidden-import=modulename
与 PyInstaller 脚本一起使用。它将modulename
默默地添加为 import 语句。
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.
如果您想指定哪个导入需要哪些附加模块,则钩子会更好。--hidden-import
作为一次性或调试更简单。
More info - https://pyinstaller.readthedocs.io/en/stable/hooks.html
更多信息 - https://pyinstaller.readthedocs.io/en/stable/hooks.html