如何正确创建 pyinstaller 钩子,或者隐藏导入? [英] How to properly create a pyinstaller hook, or maybe hidden import?
问题描述
我有两个包(比如,dataread
和 datainspector
)不知怎么被 PyInstaller 检测到了.因此,当正在运行的应用程序到达需要从这些包中导入模块时,应用程序就会终止.
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
复制到打包的应用程序中.但这会破坏打包应用程序二进制版本的意图.
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.
有什么建议吗?PS:这两个包都可能包含嵌套目录.
Any suggestions? PS: both these packages may contain nested directories.
推荐答案
Hook 是在 PyInstaller 找到 import 语句时指定附加操作的文件.
Hooks are files that specify additional actions when PyInstaller finds import statements.
如果您添加包含一行 hiddenimports = ['_proxy', 'utils', 'defs']
的 hook-data.py
文件,PyInstaller 将检查它在看到 import data
时查找其他导入.
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
.
您必须通过 --additional-hooks-dir
指定钩子目录的路径(如果您不想将源代码与仅编译文件混合,这很有用,并且而是将它们保存在单独的目录中).
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).
更简单的解决方案是将 --hidden-import=modulename
与 PyInstaller 脚本一起使用.它将以静默方式添加 modulename
作为导入语句.
The simpler solution is to use --hidden-import=modulename
along with the PyInstaller script. It will add modulename
as import statement silently.
如果你想指定哪个导入需要哪些附加模块,钩子会更好.--hidden-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.
更多信息 - https://pyinstaller.readthedocs.io/en/stable/hooks.html
这篇关于如何正确创建 pyinstaller 钩子,或者隐藏导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!