Pyinstaller and sklearn.ensemble: 'ModuleNotFoundError: No module named 'sklearn.neighbors.quad_tree' [2760]'

淺唱寂寞╮ 提交于 2020-05-13 18:55:25

问题


I have written a GUI in PyQt5 which includes the line 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:

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.

However, I get the error RecursionError: maximum recursion depth exceeded.

If, on the other hand, I just run `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'.

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.


回答1:


I ran into the same problem with sklearn and pyinstaller.

Here is how I resolved it:

1.)Use command:

> pyi-makespec -F visual_vitals.py

2.)This will create a file by name vitals.spec

3.)Find line with

> hidden imports=[]

in the spec file.

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

> import sys 
> 
> sys.setrecursionlimit(5000)

5.)Run :

> pyinstaller visual_vitals.spec



回答2:


Hope this helps anyone having

`ModuleNotFoundError: No module named 'sklearn.*'`

`ModuleNotFoundError: No module named 'h5py.*'`

During or after building pyinstaller

Example if you get an error for h5py

After running pyinstaller myscript.py a myscript.spec is generated

Go inside 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

Credit to 9dogs Pyinstaller created exe file can not load a keras nn model



来源:https://stackoverflow.com/questions/49558126/pyinstaller-and-sklearn-ensemble-modulenotfounderror-no-module-named-sklearn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!