artemis
artemis

Reputation: 7241

Pyinstaller not compiling imported modules with sklearn

I have a simple Python script, which is pasted below:

import sklearn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics

def get_average(arr):
    return sum(arr) / len(arr)

def get_median(arr):
    arr = sorted(arr)
    listlength = len(arr) 
    num = listlength//2
    if listlength%2==0:
        middlenum = (arr[num]+arr[num-1])/2
    else:
        middlenum = arr[num]
    return middlenum



iris = load_iris()
sklearn.utils.Bunch

# Data
X = iris.data
# Classes
y= iris.target
# Split data
X_train, X_test, y_train, y_test = train_test_split (X,y,test_size = 0.2, random_state = 4)


scores = []
best_k = 0
best_score = 0
for k in range (1,30):
    knn = KNeighborsClassifier(n_neighbors = k)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    score = metrics.accuracy_score(y_test, y_pred)
    if score > best_score:
        best_k = k
    scores.append(score)
    # print(scores)

print("Your scores were: ", scores)
print("The best k value was: ", k)
print("The average score was: ", get_average(scores) * 100)
print("The median score was: ", get_median(scores) * 100)

I am trying to compile that to an .exe file for my boss, who does not have Python, and run it on his machine. I have only ever compiled C and C++ code before using gcc, and Java using javac.

I tried using the pyinstaller module to compile the .exe. However, I get the following error when I go to run the .exe

> Traceback (most recent call last):   File "knn_example_iris.py", line
> 38, in <module>
>     from sklearn.neighbors import KNeighborsClassifier   File "c:\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
> line 627, in exec_module
>     exec(bytecode, module.__dict__)   File "site-packages\sklearn\neighbors\__init__.py", line 6, in <module>  
> File "sklearn\neighbors\dist_metrics.pxd", line 48, in init
> sklearn.neighbors.ball_tree   File
> "sklearn\neighbors\dist_metrics.pyx", line 52, in init
> sklearn.neighbors.dist_metrics ImportError: No module named 'typedefs'
> [12564] Failed to execute script knn_example_iris

I have tried the following steps:

I did make another example file,

print("hello world").py

which compiled and runs fine.

I have also tried recompiling the file and found many WARNING messages in the compiler I missed the first time.

> temporary directory at C:\Users\J39304\AppData\Local\Temp\tmpoxunow1d
> "C:/Users/jerry/Desktop/knn_example_iris.py" 54322 INFO: PyInstaller:
> 3.4 54322 INFO: Python: 3.5.0 54324 INFO: Platform: Windows-10.0.16299 54331 INFO: wrote
> C:\Users\jerry\AppData\Local\Temp\tmpoxunow1d\knn_example_iris.spec
> 54336 INFO: UPX is not available. 54355 INFO: Extending PYTHONPATH
> with paths ['C:\\Users\\J39304\\Desktop', 
> 'C:\\Users\\jerry\\AppData\\Local\\Temp\\tmpoxunow1d'] 54355 INFO:
> checking Analysis 54356 INFO: Building Analysis because
> Analysis-00.toc is non existent 54356 INFO: Initializing module
> dependency graph... 54359 INFO: Initializing module graph hooks...
> 54362 INFO: Analyzing base_library.zip ... 58531 INFO: running
> Analysis Analysis-00.toc 58760 WARNING: lib not found:
> api-ms-win-crt-stdio-l1-1-0.dll dependency of
> c:\python35-32\python.exe 58976 WARNING: lib not found:
> api-ms-win-crt-heap-l1-1-0.dll dependency of c:\python35-32\python.exe
> 59161 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll
> dependency of c:\python35-32\python.exe 59347 WARNING: lib not found:
> api-ms-win-crt-runtime-l1-1-0.dll dependency of
> c:\python35-32\python.exe 59554 WARNING: lib not found:
> api-ms-win-crt-locale-l1-1-0.dll dependency of
> c:\python35-32\python.exe 59745 WARNING: lib not found:
> api-ms-win-crt-stdio-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 59941 WARNING: lib not found:
> api-ms-win-crt-string-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 60129 WARNING: lib not found:
> api-ms-win-crt-convert-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 60316 WARNING: lib not found:
> api-ms-win-crt-heap-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 60499 WARNING: lib not found:
> api-ms-win-crt-runtime-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 60702 WARNING: lib not found:
> api-ms-win-crt-process-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 60883 WARNING: lib not found:
> api-ms-win-crt-stdio-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61074 WARNING: lib not found:
> api-ms-win-crt-string-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61255 WARNING: lib not found:
> api-ms-win-crt-convert-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61436 WARNING: lib not found:
> api-ms-win-crt-math-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61624 WARNING: lib not found:
> api-ms-win-crt-heap-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61804 WARNING: lib not found:
> api-ms-win-crt-runtime-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61986 WARNING: lib not found:
> api-ms-win-crt-filesystem-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62185 WARNING: lib not found:
> api-ms-win-crt-conio-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62369 WARNING: lib not found:
> api-ms-win-crt-time-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62547 WARNING: lib not found:
> api-ms-win-crt-locale-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62734 WARNING: lib not found:
> api-ms-win-crt-environment-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62741 INFO: Caching module hooks... 62748
> INFO: Analyzing C:\Users\J39304\Desktop\knn_example_iris.py 63449
> INFO: Processing pre-find module path hook   distutils 65132 INFO:
> Processing pre-find module path hook   site 65133 INFO: site:
> retargeting to fake-dir
> 'c:\\python35-32\\lib\\site-packages\\PyInstaller\\fake-modules' 67350
> INFO: Processing pre-safe import module hook  
> setuptools.extern.six.moves 78457 INFO: Loading module hooks... 78457
> INFO: Loading module hook "hook-setuptools.py"...

Upvotes: 2

Views: 1156

Answers (1)

Nqsir
Nqsir

Reputation: 899

Actually pyinstaller doesn't support the import of sklearn, see Pyinstaller Supported Packages, after multiple research, the appropriate answer seems to be a hook, but I did not spend enough time on it to explain it properly.

Instead, what worked for me, import manually the lib/methods needed, it should looks like this :

EDIT

Add this to the import list:

import sklearn.neighbors.typedefs

Run pyinstaller yourfile.py

Wait until it finishes even if there are warning and error messages.

Then we need to create two dirs in \\dist\\test\\sklearn\\datasets:

  • data in which you need to copy paste the iris.csv you can find in C:\Users\{your_name}\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\sklearn\datasets\data

  • descr in which you need to copy paste the iris.rst you can find in C:\Users\{your_name}\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\sklearn\datasets\descr

Note that this is my version of python (Python37-32)

Hope it helped

Upvotes: 2

Related Questions