問題描述
我正在使用 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 的內容并將它們分配給包含在 ma??in 中的變量,以允許此代碼在 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__'的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!