问题描述
我正在使用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钩子,并且可以通过将钩子添加到pyinstaller钩子目录中来解决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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!