10

I have this import list for my python project:

import pandas as pd
import time
import sqlalchemy
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
from sqlalchemy import Column, String, Float, Integer, SmallInteger, MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

And this spec file for distribution of the project:

import sys
sys.setrecursionlimit(5000)

block_cipher = None


a = Analysis(['DataManager.py'],
             pathex=['E:\\ForexPredictor'],
             binaries=[],
             datas=[],
             hiddenimports=['cython','pymysql','pandas._libs.tslibs.timedeltas','sklearn.neighbors.typedefs','sklearn.utils.typedefs'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='DataManager',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='DataManager')

And I use this command to make exe file of the project:

pyinstaller Datamanager.spec

But when I run the exe file it gives this error:

ModuleNotFoundError: No module named 'sklearn.utils._cython_blas'

What other things should I add to the hidden imports part?

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
amin mohammadi
  • 901
  • 2
  • 15
  • 33
  • for those who got here without pyinstaller, just `pip install sklearn.utils` and restart the kernel – AJ AJ Aug 17 '22 at 12:37

6 Answers6

27

PyInstaller uses a hook mechanism for each Python module, but sometimes it misses some internal packages so you need to provide them manually. You can use --hidden-import to add sklearn's missing modules.

pyinstaller -F --hidden-import="sklearn.utils._cython_blas" --hidden-import="sklearn.neighbors.typedefs" --hidden-import="sklearn.neighbors.quad_tree" --hidden-import="sklearn.tree._utils" Datamanager.py
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • 7
    This works but with every build I can only discover one missing package, and then I have to build with it to get the next missing one. Isn't there a way to know all of them at once? – astackoverflowuser Feb 27 '21 at 20:38
  • @Eren this is a great question! I hope someone can help with this – Baggio Aug 06 '21 at 19:06
4

Add

import sklearn.utils._cython_blas

and maybe

import sklearn.neighbors.typedefs
import sklearn.neighbors.quad_tree
import sklearn.tree
import sklearn.tree._utils

to your code.

nda
  • 541
  • 7
  • 18
4

I haven't tried but think you will solve this by adding: --collect-submodules "sklearn"

I was using "Auto py to exe", which uses pyinstaller internally and solved this modifying that parameter.

If you also use "Auto py to exe" here are two ways to solve this problem.

  1. (This one explains what the problem is) First run the installer without selecting the "one file" option (choose one directory). Once the process has finished, open the containing folder. There you will find the sklearn folder, which hasn't been fully copied by pyinstaller. So, what's going to solve the problem is replacing this folder with the one found in "C:\PythonXY\Lib\site-packages" with the same name "sklearn". After that, the exe file will run without problems.

  2. (I prefer this one) If you also want to collect everything into the exe file using the one file you wont be able to replace the sklearn folder manually. In this case, you have to write "sklearn" (without "") in " Advanced>what to bundle, where to search>collect-submodules"

1

It is work for my code after i execute this following code :

pyinstaller --hidden-import="sklearn.utils._cython_blas" --hidden-import="sklearn.neighbors.typedefs" --hidden-import="sklearn.neighbors.quad_tree" --hidden-import="sklearn.tree._utils" --hidden-import="sklearn.neighbors._typedefs" --hidden-import="sklearn.utils._weight_vector" --hidden-import="sklearn.neighbors._quad_tree" namepythonfile.py

Try to add all the module to hidden import code to be successful

0

Have you tried reading the documentation regarding the use of .spec-files? https://pyinstaller.readthedocs.io/en/stable/spec-files.html#using-spec-files

I don't know if it's the issue, but where is your *.py-file in the command for creating an .exe-file? As far as I know you have to give pyinstaller a .py-file for it to create a program.

BHR
  • 33
  • 6
  • Yes I have read it. I think the problem is with the hidden imports part. I don't know what other things I should add to this part. – amin mohammadi Jul 19 '19 at 08:23
0

I had the same issue using auto_py_to_ex... Got it solved by copying the sklearn folder from the program data/anaconda3/env/sitepackages to the mian folder and it worked

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 17 '23 at 14:17