您如何解决“找不到隐藏的导入!"pyinstaller 中 scipy 的警告? [英] How do you resolve 'hidden imports not found!' warnings in pyinstaller for scipy?
问题描述
我正在使用 pyinstaller 为使用 Pandas 和 sklearn 的 python 程序创建一个 .exe.pyinstaller 进程按预期完成并生成带有可执行文件的 dist 文件夹.但是,当我运行 .exe 时,出现与 sklearn 和 scipy 相关的模块导入错误.
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.
我创建了一个测试脚本(test.py)来测试导入,它只导入pandas和sklearn然后打印成功信息:
I created a test script (test.py) to test imports, which only imports pandas and sklearn and then prints a success message:
import time
import pandas as pd
import sklearn
def main():
print('hello world!')
time.sleep(5)
if __name__ == '__main__':
main()
我知道 pyinstaller hooks 并且我能够通过向 pyinstaller hooks 目录添加一个钩子来解决 pandas 错误.我为 sklearn 和 scipy 添加了类似的钩子,看起来它们正在运行,但是在 pyinstaller 输出中,我收到了找不到隐藏导入sklearn.utils.sparsetools._graph_validation"的警告!和类似的'._graph_tools'.
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'.
这是 scipy 的钩子 (hook-scipy.py):
Here's the hook for scipy (hook-scipy.py):
print('loading custome hook for scipy')
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('scipy')
这是运行 pyinstaller 生成的警告的快照
Here's a snapshot of the warnings generated from running pyinstaller
这是运行 test.exe 时的错误快照
Here's a snapshot of the error when running test.exe
我在一个安装了 pyinstaller、pandas、sklearn、scipy 和所有依赖项的虚拟环境中工作(至少我可以在这个 venv 中运行常规的 test.py 脚本).在 Windows 10.10.0 上使用 PyInstaller 3.3.1、Python 3.6.4.
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.
感谢任何帮助!
推荐答案
你需要进入 hook-scipy.py(或创建一个)并让它看起来像这样:
You need to go into the hook-scipy.py (or create one) and have it look like this:
from PyInstaller.utils.hooks import collect_submodules
from PyInstaller.utils.hooks import collect_data_files
hiddenimports = collect_submodules('scipy')
datas = collect_data_files('scipy')
然后进入 hook-sklearn.metrics.cluster.py 文件并修改它看起来像这样:
then go into the hook-sklearn.metrics.cluster.py file and modify it to look like this:
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')
我不知道这部分是否有必要,但我也创建了一个看起来像这样的 hook-sklearn.py 文件:
I do not know if this part is necessary but I also created a hook-sklearn.py file that looks like this:
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('sklearn')
在 cmd 中我使用 pyinstaller test.py -F
创建一个文件.
In the cmd I used pyinstaller test.py -F
to create one file.
那么它应该可以工作:
这篇关于您如何解决“找不到隐藏的导入!"pyinstaller 中 scipy 的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!