简体   繁体   English

Python多处理错误:AttributeError:模块'__main__'没有属性'__spec__'

[英]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]))

pdb users pdb 用户

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.

相关问题 模块'__main__'在jupyter中没有属性'__spec__' - module '__main__' has no attribute '__spec__' in jupyter Python 多处理错误:AttributeError: Can't get attribute 'task' on<module '__main__' (built-in)> "</module> - Python Multiprocessing error: AttributeError: Can't get attribute 'task' on <module '__main__' (built-in)>" AttributeError: 模块 '__main__' 没有属性 'AverageWordLengthExtractor' - AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor' AttributeError:模块'__main__'没有属性'cleaner' - AttributeError: module '__main__' has no attribute 'cleaner' 多重处理错误“ AttributeError:无法获取属性&#39;testfuncxx&#39; - multiprocessing error “AttributeError: Can't get attribute 'testfuncxx' on <module '__main__” unittest:AttributeError:模块'__main__'没有属性'C:\ ...' - unittest: AttributeError: module '__main__' has no attribute 'C:\…' 查找“pip”的模块规范时出错(AttributeError:模块“__main__”没有属性“__file__”) - Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__') pickle/joblib AttributeError: 模块 &#39;__main__&#39; 在 pytest 中没有属性 &#39;thing&#39; - pickle/joblib AttributeError: module '__main__' has no attribute 'thing' in pytest 用于单元测试的python3:AttributeError:模块&#39;__main__&#39;没有属性“内核......” - python3 for unit test: AttributeError: module '__main__' has no attribute “kernel…” 如何修复 pip 错误“查找 'pip' 的模块规范时出错(AttributeError:模块 '__main__' 没有属性 '__file__')”? - How to fix pip error "Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__')"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM