简体   繁体   English

如何正确创建 pyinstaller 钩子,或者隐藏导入?

[英]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.我有两个包(比如datareaddatainspector ),它们以某种方式没有被 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.最简单的解决方案是将datareaddatainspector复制到打包的应用程序中。 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. PS:这两个包都可能包含嵌套目录。

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时检查它以查找其他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.更简单的解决方案是将--hidden-import=modulename与 PyInstaller 脚本一起使用。 It will add modulename as import statement silently.它将以静默方式添加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

Use Auto-py-to-exe to add hindden libraries.使用Auto-py-to-exe添加隐藏库。 In additional files add Rasterio folder (C:/users/Admin/anaconda3/envs/name/Lib/site-packages/rasterio) and then convert to exe.其他文件中添加Rasterio文件夹(C:/users/Admin/anaconda3/envs/name/Lib/site-packages/rasterio) ,然后转换为 exe。

It worked for me.它对我有用。 if it says some other libraries are missing then add folder of that library too.如果它说缺少其他一些库,则也添加该库的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM