Python Multiprocessing error: AttributeError: module #39;__main__#39; has no attribute #39;__spec__#39;(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: 模块 '__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,但我只发现了另一种这种错误的情况,它没有答案.
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__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python多处理错误:AttributeError:模块'__main__'没有属性'__spec__'
基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01