gpt4 book ai didi

Python 多处理错误 : AttributeError: module '__main__' has no attribute '__spec__'

转载 作者:IT老高 更新时间:2023-10-28 21:18:49 28 4
gpt4 key购买 nike

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

错误信息:AttributeError: 模块 '__main__' 没有属性 '__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]))

我尝试用谷歌搜索它并搜索 Stack Overflow,但我只发现了另一种这种错误的情况,它没有答案。

最佳答案

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

经过一番调查,我发现代码在外部系统终端中执行时运行良好,但在 Spyder 的 IPython 控制台中运行时却不行。

我能够转储 spec 的内容并将它们分配给包含在 ma​​in 中的变量,以允许此代码在 IPython 控制台中运行。

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: module '__main__' has no attribute '__spec__' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45720153/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com