[英]Pyinstaller and sklearn.ensemble: 'ModuleNotFoundError: No module named 'sklearn.neighbors.quad_tree' [2760]'
I have written a GUI in PyQt5 which includes the line from sklearn.ensemble import RandomForestClassifier
. 我在PyQt5中编写了一个GUI,其中包含
from sklearn.ensemble import RandomForestClassifier
的行。
Following the suggestion in this answer , in \\Anaconda3\\Lib\\site-packages\\PyInstaller\\hooks
, I have added a file called hook-pandas.py
which contains the following: 根据这个答案的建议,在
\\Anaconda3\\Lib\\site-packages\\PyInstaller\\hooks
,我添加了一个名为hook-pandas.py
的文件,其中包含以下内容:
hiddenimports = ['pandas._libs.tslibs.timedeltas', 'sklearn.neighbors.typedefs']
After that, I tried running pyinstaller -F visual_vitals.py --hidden-import sklearn.neighbors.typedefs
in the Anaconda Prompt. 之后,我尝试在Anaconda Prompt中运行
pyinstaller -F visual_vitals.py --hidden-import sklearn.neighbors.typedefs
。
However, I get the error RecursionError: maximum recursion depth exceeded
. 但是,我收到错误
RecursionError: maximum recursion depth exceeded
。
If, on the other hand, I just run `pyinstaller visual_vitals.py' 另一方面,如果我只运行`pyinstaller visual_vitals.py'
then the .exe builds correctly, when I try running it, I get the message modulenotfounderror: no module named 'sklearn.neighbors.quad_tree'
. 然后.exe构建正确,当我尝试运行它时,我得到消息
modulenotfounderror: no module named 'sklearn.neighbors.quad_tree'
。
What can I do about that? 我该怎么办?
Note that the problem disappears if, instead of a random forest, I use a support vector classifier, so the problem is specific to this classifier rather than to the whole of sklearn
. 请注意,如果我使用支持向量分类器而不是随机林,问题就会消失,因此问题是特定于此分类器而不是整个
sklearn
。
I ran into the same problem with sklearn and pyinstaller. 我遇到了与sklearn和pyinstaller相同的问题。
Here is how I resolved it: 以下是我如何解决它:
1.)Use command: 1.)使用命令:
> pyi-makespec -F visual_vitals.py
2.)This will create a file by name vitals.spec 2.)这将按名称vitals.spec创建一个文件
3.)Find line with 3.)查找行
> hidden imports=[]
in the spec file. 在spec文件中。
Replace it with 替换为
> hiddenimports = ['pandas._libs.tslibs.timedeltas',
> 'sklearn.neighbors.typedefs']
4.)Add these two lines to increase recursion limit at beginning of the spec file 4.)添加这两行以增加spec文件开头的递归限制
> import sys
>
> sys.setrecursionlimit(5000)
5.)Run : 5.)运行:
> pyinstaller visual_vitals.spec
Hope this helps anyone having 希望这可以帮助任何人
`ModuleNotFoundError: No module named 'sklearn.*'`
`ModuleNotFoundError: No module named 'h5py.*'`
During or after building pyinstaller 在构建pyinstaller期间或之后
Example if you get an error for h5py
例如,如果您收到
h5py
的错误
After running pyinstaller myscript.py
a myscript.spec
is generated 运行
pyinstaller myscript.py
后,会生成一个myscript.spec
Go inside myscript.spec
进入
myscript.spec
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['myscript.py'],
binaries=None,
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None)
# ... rest of a file untouched
Add 加
from PyInstaller.utils.hooks import collect_submodules
hidden_imports = collect_submodules('h5py')
and 和
hiddenimports=hidden_imports,
Like this 像这样
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
from PyInstaller.utils.hooks import collect_submodules
hidden_imports = collect_submodules('h5py')
a = Analysis(['myscript.py'],
binaries=None,
datas=[],
hiddenimports=hidden_imports,
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None)
# ... rest of a file untouched
Then Save myscript.spec
and run command pyinstaller myscript.spec
然后保存
myscript.spec
并运行命令pyinstaller myscript.spec
Credit to 9dogs Pyinstaller created exe file can not load a keras nn model 感谢9dogs Pyinstaller创建的exe文件无法加载keras nn模型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.