[英]Python Multiprocessing error: AttributeError: module '__main__' has no attribute '__spec__'
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我正在使用 Python 3.6,并尝试遵循下面网站上的第一个示例(完整代码也在下面)并且收到以下错误: https://docs.python.org/3.6/library/multiprocessing。 html
Error message: AttributeError: module '__main__' has no attribute '__spec__'
错误消息:
AttributeError: module '__main__' has no attribute '__spec__'
Full example code:完整示例代码:
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]))
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.我尝试用谷歌搜索它并搜索 Stack Overflow,但我只发现了另一种这种错误的情况,它没有答案。
The problem is not with the code / Python 3.6, it is with Spyder.问题不在于代码/Python 3.6,而在于 Spyder。
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.经过一番调查,我发现代码在外部系统终端中执行时运行良好,但在 Spyder 的 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.我能够转储spec的内容并将它们分配给main中包含的变量,以允许此代码在 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]))
The question didn't specifically mention Spyder nor Conda (though it is tagged as such).这个问题没有特别提到 Spyder 或 Conda(尽管它被标记为这样)。 Hence, I will note that I found this can also happen when using pdb.
因此,我会注意到我发现在使用 pdb 时也会发生这种情况。
Eg例如
python -m pdb myprogram.py
Passing __spec__ = None
would be a useful workaround if you wanted to persist with pdb.如果您想坚持使用 pdb,传递
__spec__ = None
将是一个有用的解决方法。
the same probelm in Spyder (Anaconda3, python 3.6) when I try the external terminal.当我尝试外部终端时,Spyder(Anaconda3,python 3.6)中的相同问题。
Error message: AttributeError: module '__main__' has no attribute '__spec__'
I changed the Run console to 'Excute in current console', and applied it.我将运行控制台更改为“在当前控制台中执行”,并应用它。 then if that doesnot work, try other conselor and then change back to 'Excute in current console'.
然后如果这不起作用,请尝试其他 conselor,然后改回“在当前控制台中执行”。 Finally, it works.
最后,它起作用了。 no
'__spec__ = None'
is needed.不需要
'__spec__ = None'
。
Same problem with Spyder (Anaconda3, python 3.7). Spyder(Anaconda3,python 3.7)也有同样的问题。
I used我用了
from genetic_selection import GeneticSelectionCV
def main(): .... and as I was running the code, an error like this occured: def main(): .... 当我运行代码时,发生了这样的错误:
main_mod_name = getattr(main_module.__spec__, "name", None)
AttributeError: module '__main__' has no attribute '__spec__'
what I did is deleted "__spec__"
in main_mod_name = getattr(main_module.__spec__, "name", None)
我所做的是在
main_mod_name = getattr(main_module.__spec__, "name", None)
中删除了"__spec__"
so I only have this: main_mod_name = getattr(main_module, "name", None)
所以我只有这个:
main_mod_name = getattr(main_module, "name", None)
the code then worked perfectly fine.然后代码工作得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.