ignoring_gravity
ignoring_gravity

Reputation: 10511

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.

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.

Upvotes: 2

Views: 6626

Answers (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

Upvotes: 2

Sumit Paroothi
Sumit Paroothi

Reputation: 101

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

Upvotes: 5

Related Questions