Python多重处理错误:AttributeError:模块'__main__'没有属性'__spec__' [英] Python Multiprocessing error: AttributeError: module '__main__' has no attribute '__spec__'

查看:311
本文介绍了Python多重处理错误:AttributeError:模块'__main__'没有属性'__spec__'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.6,并尝试遵循下面网站上的第一个示例(下面也提供完整代码),并出现以下错误: https://docs.python.org/3.6/library/multiprocessing.html

I'm using Python 3.6 and am trying to follow along with the very first example at the website below (full code also below) and am getting the below error: https://docs.python.org/3.6/library/multiprocessing.html

错误消息: 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]))

我尝试使用Google搜索,然后搜索Stack Overflow,但我只发现了另一种情况,但此错误没有答案.

I tried Googling it and searching Stack Overflow but I've only found one other case of this error and it did not have an answer.

推荐答案

问题不在于代码/Python 3.6,而在于Spyder.

The problem is not with the code / Python 3.6, it is with Spyder.

经过一番调查,我发现该代码在外部系统终端中执行时可以正常运行,而在Spyder的IPython控制台中运行时则不能正常运行.

After some investigation I found that the code runs fine when executed in an external system terminal but not when run in Spyder's IPython console.

我能够转储 spec 的内容,并将其分配给 main 中包含的变量,以使该代码在IPython控制台中起作用.

I was able to dump the contents of spec and assign them to a variable that was included inside main to allow this code to function within the IPython console.

from multiprocessing import Pool

def f(x):
    return x*x

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

这篇关于Python多重处理错误:AttributeError:模块'__main__'没有属性'__spec__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆