Why can't I create a ThreadPool in cx_Freeze?

Viewed 819
Traceback (most recent call last):

  File "C:\Users\Jun\AppData\Local\Programs\Python\Python35\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run

    module.run()

  File "C:\Users\Jun\AppData\Local\Programs\Python\Python35\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run

    exec(code, m.__dict__)

  File "D:/ruanjian/new/Ui-Design/hand_up_625.py", line 25, in <module>

    from keras.models import load_model

  File "C:\Users\Jun\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\__init__.py", line 3, in <module>

    from . import utils

  File "C:\Users\Jun\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\utils\__init__.py", line 4, in <module>

    from . import data_utils

  File "C:\Users\Jun\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\utils\data_utils.py", line 19, in <module>

    from multiprocessing.pool import ThreadPool

ImportError: No module named 'multiprocessing.pool'
2 Answers

Try to add 'multiprocessing' to the packages list of the build_exe_options in the setup.py script:

build_exe_options = {"packages": ['multiprocessing']}

# ...

setup(  name = ...,  # complete!
        ...
        options = {"build_exe": build_exe_options},
        executables = [Executable(...)])

See the cx_Freeze documentation for further details.

Please also notice that you will have to call multiprocessing.freeze_support() for your frozen script to keep working with multiprocessing. According to the documentation:

Add support for when a program which uses multiprocessing has been frozen to produce a Windows executable. (Has been tested with py2exe, PyInstaller and cx_Freeze.)

One needs to call this function straight after the if __name__ == '__main__' line of the main module. For example:

from multiprocessing import Process, freeze_support

def f():
    print('hello world!')

if __name__ == '__main__':
    freeze_support()
    Process(target=f).start()

If the freeze_support() line is omitted then trying to run the frozen executable will raise RuntimeError.

Calling freeze_support() has no effect when invoked on any operating system other than Windows. In addition, if the module is being run normally by the Python interpreter on Windows (the program has not been frozen), then freeze_support() has no effect.

Related