2017-08-16 52 views
7

我正在使用Python 3.6,並試圖遵循下面的網站(完整代碼也在下面)的第一個例子,以下錯誤: https://docs.python.org/3.6/library/multiprocessing.htmlPython多處理錯誤:AttributeError:模塊'__main__'沒有屬性'__spec__'

錯誤消息: AttributeError: module '__main__' has no attribute '__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])) 

我想谷歌搜索,並在搜索堆棧Overflo但我只發現了另一個這種錯誤的情況,它沒有答案。

+0

張貼的代碼工作正常,我的機器 – bendl

+0

我使用蟒蛇/ Spyder的與Python 3.6 ...也許有什麼關係呢? – user8474060

+0

我在Spyder 3.1.4上使用Spyder 3.1.2和Python 3.6.0(Anaconda 4.3.1)在Windows 7中 – bendl

回答

13

問題不在於代碼/ Python 3.6,它與Spyder。

經過一番調查後,我發現代碼在外部系統終端中執行時運行良好,但在Spyder的IPython控制檯中運行時無法運行。

我能夠轉儲規範的內容,並將其分配給所包含內部主要允許該代碼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])) 
+6

我真的很驚訝,因爲'__spec__'通常不是一個字符串。既然它確實有效,你可以使用'None'。 – Kevin

+0

我試過'__spec__ = __spec__',我的子進程開始無限地停止。腳本在系統終端啓動時,我也檢查了'__spec__'變量。這是'沒有'。所以我認爲'__spec__ = None'是解決這個問題的「正確」方法。 – Winand

相關問題