2014-04-02 58 views
1

我在我的Windows 7計算機上安裝了Python 2.7.3。當我運行下面的代碼cPickle.load在Python中拋出ImportError

import nltk, json, cPickle, itertools 

import numpy as np 

from nltk.tokenize import word_tokenize 
from pprint import pprint 

t_given_a = json.load(open('conditional_probability.json','rb')) 
a_unconditional = json.load(open('age.json','rb')) 

t_unconditional = cPickle.load(open('freqdist.pkl','rb'))['distribution'] 

命令提示符給我的錯誤 「導入錯誤:未命名的多陣列模塊」。

我對Python很新,我不完全確定爲什麼發生這個錯誤。我搜索了其他線程,許多人建議使用'rb'而不是'r'。我有rb開始,它仍然拋出我的錯誤。任何建議?

+0

哪裏是泡菜文件從何而來?爲了解開某些東西,你需要加載任何需要的庫來重新創建被醃製的對象。這聽起來像你正試圖在沒有合適的庫的情況下取消其他人的醃菜。 – BrenBarn

回答

3

當你在python中醃製一個對象時,它把它的類保存爲包名+類名的字符串。在unpickle python試圖導入該模塊,並找到該類爲您重新創建一個對象。如果你沒有那個可導入的模塊,你會得到一個ImportError。

只需安裝Multiarray模塊,並且如果您不知道它是哪一個模塊,那麼請問您從誰那裏獲得了該pickle文件。

1

docs

Note that functions (built-in and user-defined) are pickled by 「fully qualified」 name reference, not by value. This means that only the function name is pickled, along with the name of the module the function is defined in. Neither the function’s code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised.

Similarly, classes are pickled by named reference, so the same restrictions in the unpickling environment apply. Note that none of the class’s code or data is pickled

[...] These restrictions are why picklable functions and classes must be defined in the top level of a module