首页 > 解决方案 > 为什么我不能在 cx_Freeze 中创建线程池?

问题描述

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-Disign/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'

标签: python-3.xkeraspython-multiprocessingcx-freeze

解决方案


尝试添加'multiprocessing'到脚本中的packages列表:build_exe_optionssetup.py

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

# ...

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

有关详细信息,请参阅 cx_Freeze文档

另请注意,您必须调用multiprocessing.freeze_support()冻结脚本才能继续使用多处理。根据文档

添加对使用多处理的程序被冻结以生成 Windows 可执行文件时的支持。(已使用py2exePyInstallercx_Freeze进行测试。)

需要if __name__ == '__main__'在主模块的行之后直接调用此函数。例如:

from multiprocessing import Process, freeze_support

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

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

如果freeze_support()省略该行,则尝试运行冻结的可执行文件将引发 RuntimeError。

freeze_support()在 Windows 以外的任何操作系统上调用时调用无效。另外,如果模块在 Windows 上被 Python 解释器正常运行(程序没有被冻结),则freeze_support()没有效果。


推荐阅读