Search code examples
numpyscikit-learnpython-3.9auto-py-to-exe

Python 3.9 , autopytoexe, numpy & sklearn gives me a problem. How to solve this?


I'm using auto-py-to-exe with python 3.9.13 but when running autopy, to make a single file, it gives me this warning:


C:\Users\jsgas\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py:174: UserWarning:

The numpy.array_api submodule is still experimental. See NEP 47.



4597 INFO: Extra DLL search directories (AddDllDirectory): ['C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\numpy\\.libs', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\scipy.libs']
4597 INFO: Extra DLL search directories (PATH): ['C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem', 'C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\WINDOWS\\System32\\OpenSSH\\', 'C:\\Users\\jsgas\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Python 3.9', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\DLLs', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Scripts', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\numpy\\.libs', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\scipy.libs', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Scripts\\', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\', 'C:\\Users\\jsgas\\AppData\\Local\\Microsoft\\WindowsApps', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Microsoft VS Code\\bin', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\DLLs', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Scripts', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\numpy\\.libs', 'C:\\Users\\jsgas\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\scipy.libs', 'C:\\Users\\jsgas\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Python 3.9']

This is just a warning and not an error, but when running the exe, it gives errors and doesn't run.

this is the error:

  File "sklearn\cluster\_spectral.py", line 20, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "sklearn\metrics\__init__.py", line 42, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "sklearn\metrics\cluster\__init__.py", line 22, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "sklearn\metrics\cluster\_unsupervised.py", line 16, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "sklearn\metrics\pairwise.py", line 33, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "sklearn\metrics\_pairwise_distances_reduction\__init__.py", line 89, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 11, in <module>
  File "sklearn\metrics\_pairwise_distances_reduction\_base.pyx", line 1, in init sklearn.metrics._pairwise_distances_reduction._base
ModuleNotFoundError: No module named 'sklearn.metrics._pairwise_distances_reduction._datasets_pair'

I've tried adding those directories to the variables of the user and the computer but the warning still appears.

I have installed scikit-learn-1.2.0 (the last one), and I use an example from the documents for KMeans in the app (can't show the code, but VS shows as the modules are imported, and when running the app, it doesn't give problems (as a .py)). The problems appears when running the exe.

I don't know what more to do.

Please help.

simple example (this should not let me make a propper exe (this would give errors)

from sklearn.cluster import KMeans
import numpy as np
X = np.array([[1, 2], [1, 4], [1, 0],
...               [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0, 
n_init="auto").fit(X)
kmeans.labels_
array([1, 1, 1, 0, 0, 0], dtype=int32)
kmeans.predict([[0, 0], [12, 3]])
array([1, 0], dtype=int32)
kmeans.cluster_centers_
array([[10.,  2.],
       [ 1.,  2.]])

Solution

  • Using your example I was able to compile using the following steps.

    1. Create a clean virtual enviornment and install sklearn and pyinstaller
    python -m venv venv
    venv\scripts\activate
    python -m pip install --upgrade pip pyinstaller scikit-learn
    
    1. I copied your program into main.py with some minor alterations.
    from sklearn.cluster import KMeans
    import numpy as np
    
    if __name__ == "__main__":
        X = np.array([[1, 2], [1, 4], [1, 0],
                    [10, 2], [10, 4], [10, 0]])
        kmeans = KMeans(n_clusters=2, random_state=0,
        n_init="auto").fit(X)
        print(kmeans.labels_)
        # array([1, 1, 1, 0, 0, 0], dtype=int32)
        print(kmeans.predict([[0, 0], [12, 3]]))
        # array([1, 0], dtype=int32)
        print(kmeans.cluster_centers_)
    
        # array([[10.,  2.],
            #    [ 1.,  2.]])
    
    1. Run pyinstaller -F --collect-all sklearn main.py

    2. run dist\main.exe and I got this as output.

    [1 1 1 0 0 0]
    [1 0]
    [[10.  2.]
     [ 1.  2.]]
    

    For auto-py-to-exe you would do the exact same thing.

    After selecting your entry point and choosing all of your other settings, click on the advanced tab and choose collect-all and add sklearn in the line edit field. Then compile the program.

    enter image description here