sklearn.model_selection.cross_val_score + Pyqt cause a restart of the execution in Exe file created with cx_Freeze

Viewed 17

Describe the bug Since a few days I'm trying to deal with a problem that only happens when compiling a code with cx_Freeze. I am currently developing a code that makes use of the sklearn.model_selection.cross_val_score function, using the "n_jobs=-1" parameter that causes all processor cores to be used to perform this function. I am running this code using a graphical interface created with PyQt5. When I run the code directly from the .py file the execution is normal, but when I compile the files with cx_Freeze and run the .exe file, when the code arrives to the cross_val_score function the process restarts, executing again as many times as cores the processor has. After a long time, I have managed to isolate the problem, and I have managed to reproduce a very simple code that causes the problem. Do you have any idea how to solve it?

To Reproduce test.py

import sklearn
from sklearn import neural_network
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')
        button.move(100,70)
        button.clicked.connect(self.on_click)
        self.show()
    @pyqtSlot()
    def on_click(self):
        print('PyQt5 button click')
        from sklearn import datasets
        from sklearn.svm import SVC
        iris = datasets.load_iris()
        clf = sklearn.neural_network.MLPClassifier()
        clf.fit(iris.data, iris.target)
        list(clf.predict(iris.data[:3]))
        clf.fit(iris.data, iris.target_names[iris.target])
        print(list(clf.predict(iris.data[:3])))
        scores = sklearn.model_selection.cross_val_score(clf, iris.data, iris.target_names[iris.target], cv=50, n_jobs=-1)
        print("Scores:")
        print(scores)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

setup.py

from cx_Freeze import setup, Executable
import requests

os.environ['TCL_LIBRARY'] = 'C:/Python37/tcl/tcl8.6'
os.environ['TK_LIBRARY'] = 'C:/Python37/tcl/tk8.6'


include_files = ['C:/Python37/DLLs/tcl86t.dll', 'C:/Python37/DLLs/tk86t.dll']
packages = ["os", "laspy","sys","numpy","pptk","keyboard","multiprocessing","pandas","matplotlib","seaborn","sklearn","tkinter","laspy.file","matplotlib.pyplot","matplotlib.colors","scipy"]

setup(
    name = "Clasex",
    options = {"build_exe": {
    'packages': packages,
    'include_files': include_files,
    'include_msvcr': True,
    'add_to_path': True,
    'includes':["tkinter"]
}},
executables = [Executable(script="test.py")]
)

Exe file is created executing "python setup.py bdist_msi"

Desktop:

  • Windows 10
  • Intel64
  • cx_Freeze version [6.9]:
  • Python version [3.7.9]:
0 Answers
Related