pandas 您如何解决“找不到隐藏的导入!” pyinstaller 中 scipy 的警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49559770/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:23:27  来源:igfitidea点击:

How do you resolve 'hidden imports not found!' warnings in pyinstaller for scipy?

pythonpandasscipyscikit-learnpyinstaller

提问by Luke

I'm working on using pyinstaller to create an .exe for a python program that uses pandas and sklearn. The pyinstaller process completes and produces the dist folder with the executable as expected. However, when I run the .exe I get module import errors related to sklearn and scipy.

我正在使用 pyinstaller 为使用 Pandas 和 sklearn 的 python 程序创建一个 .exe。pyinstaller 进程按预期完成并生成带有可执行文件的 dist 文件夹。但是,当我运行 .exe 时,出现与 sklearn 和 scipy 相关的模块导入错误。

I created a test script (test.py) to test imports, which only imports pandas and sklearn and then prints a success message:

我创建了一个测试脚本 (test.py) 来测试导入,它只导入 pandas 和 sklearn,然后打印成功消息:

import time
import pandas as pd
import sklearn

def main():
  print('hello world!')
  time.sleep(5)


if __name__ == '__main__':
  main()

I'm aware of pyinstaller hooks and I was able to resolve the pandas errors by adding a hook to the pyinstaller hooks directory. I added similar hooks for sklearn and scipy it looks like they're running, but in the pyinstaller output I'm getting warnings that 'Hidden import "sklearn.utils.sparsetools._graph_validation" not found!' and similar one for '._graph_tools'.

我知道 pyinstaller hooks 并且我能够通过向 pyinstaller hooks 目录添加一个钩子来解决 pandas 错误。我为 sklearn 和 scipy 添加了类似的钩子,看起来它们正在运行,但是在 pyinstaller 输出中,我收到了“找不到隐藏导入“sklearn.utils.sparsetools._graph_validation”的警告!和类似的'._graph_tools'。

Here's the hook for scipy (hook-scipy.py):

这是 scipy 的钩子 (hook-scipy.py):

print('loading custome hook for scipy')

from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('scipy') 

Here's a snapshot of the warnings generated from running pyinstaller

这是运行 pyinstaller 生成的警告的快照

Here's a snapshot of the error when running test.exe

这是运行 test.exe 时的错误快照

I'm working in a virtual environment where pyinstaller, pandas, sklearn, scipy and all dependencies are installed (at least I can get the regular test.py script running in this venv). Using PyInstaller 3.3.1, Python 3.6.4 on Windows 10.10.0.

我在一个安装了 pyinstaller、pandas、sklearn、scipy 和所有依赖项的虚拟环境中工作(至少我可以在这个 venv 中运行常规的 test.py 脚本)。在 Windows 10.10.0 上使用 PyInstaller 3.3.1、Python 3.6.4。

Any help is appreciated!

任何帮助表示赞赏!

采纳答案by Yo_Chris

You need to go into the hook-scipy.py (or create one) and have it look like this:

您需要进入 hook-scipy.py(或创建一个)并使其看起来像这样:

from PyInstaller.utils.hooks import collect_submodules
from PyInstaller.utils.hooks import collect_data_files
hiddenimports = collect_submodules('scipy')

datas = collect_data_files('scipy')

then go into the hook-sklearn.metrics.cluster.py file and modify it to look like this:

然后进入 hook-sklearn.metrics.cluster.py 文件并将其修改为如下所示:

from PyInstaller.utils.hooks import collect_data_files

hiddenimports = ['sklearn.utils.sparsetools._graph_validation',
                 'sklearn.utils.sparsetools._graph_tools',
                 'sklearn.utils.lgamma',
                 'sklearn.utils.weight_vector']

datas = collect_data_files('sklearn')

I do not know if this part is necessary but I also created a hook-sklearn.py file that looks like this:

我不知道这部分是否必要,但我还创建了一个 hook-sklearn.py 文件,如下所示:

from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('sklearn')

In the cmd I used pyinstaller test.py -Fto create one file.

在 cmd 我曾经pyinstaller test.py -F创建一个文件。

Then it should work:

然后它应该工作:

enter image description here

在此处输入图片说明