AttributeError: module ‘__main__‘ has no attribute ‘__spec__‘

遇到这个问题是因为在python多进程过程中不能插入断点debug,出现了此报错信息。查找之后在main方法中添加语句解决。

 

https://stackoverflow.com/questions/45720153/python-multiprocessing-error-attributeerror-module-main-has-no-attribute

错误信息: AttributeError: module ‘main’ has no attribute ‘spec

完整代码:

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))

解决方法

增加一行  __spec__ = "ModuleSpec(name='builtins', loader=)" 即可  
  •  

修改后代码

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    __spec__ = "ModuleSpec(name='builtins', loader=)"
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))

你可能感兴趣的:(Python,python)