2010-06-09 98 views
4

我正在使用cPickle將我的數據庫保存到文件中。代碼看起來像這樣:cPickle ImportError:No module named multiarray

def Save_DataBase(): 
import cPickle 
from scipy import * 
from numpy import * 
a=Results.VersionName 
#filename='D:/results/'+a[a.find('/')+1:-a.find('/')-2]+Results.AssType[:3]+str(random.randint(0,100))+Results.Distribution+".lft" 
filename='D:/results/pppp.lft' 
plik=open(filename,'w') 


DataOutput=[[[DataBase.Arrays.Nodes,DataBase.Arrays.Links,DataBase.Arrays.Turns,DataBase.Arrays.Connectors,DataBase.Arrays.Zones], 
      [DataBase.Nodes.Data,DataBase.Links.Data,DataBase.Turns.Data,DataBase.OrigConnectors.Data,DataBase.DestConnectors.Data,DataBase.Zones.Data], 
      [DataBase.Nodes.DictionaryPy2Vis,DataBase.Links.DictionaryPy2Vis,DataBase.Turns.DictionaryPy2Vis,DataBase.OrigConnectors.DictionaryPy2Vis,DataBase.DestConnectors.DictionaryPy2Vis,DataBase.Zones.DictionaryPy2Vis], 
      [DataBase.Nodes.DictionaryVis2Py,DataBase.Links.DictionaryVis2Py,DataBase.Turns.DictionaryVis2Py,DataBase.OrigConnectors.DictionaryVis2Py,DataBase.DestConnectors.DictionaryVis2Py,DataBase.Zones.DictionaryVis2Py], 
      [DataBase.Paths.List]],[Results.VersionName,Results.noZones,Results.noNodes,Results.noLinks,Results.noTurns,Results.noTrips, 
            Results.Times.VersionLoad,Results.Times.GetData,Results.Times.GetCoords,Results.Times.CrossTheTime,Results.Times.Plot_Cylinder, 
            Results.AssType,Results.AssParam,Results.tStart,Results.tEnd,Results.Distribution,Results.tVector]] 



cPickle.dump(DataOutput, plik, protocol=0) 
plik.close()` 

它工作正常。我的大部分數據庫行都是列表,類似vecor的數組或類似數組的數據集。

但現在當我輸入數據,發生錯誤:

def Load_DataBase(): 
    import cPickle 
    from scipy import * 
    from numpy import * 
    filename='D:/results/pppp.lft' 
    plik= open(filename, 'rb') 
    """ first cPickle load approach """ 
    A= cPickle.load(plik) 
    """ fail """ 
    """ Another approach - data format exact as in Output step above , also fails""" 
    [[[DataBase.Arrays.Nodes,DataBase.Arrays.Links,DataBase.Arrays.Turns,DataBase.Arrays.Connectors,DataBase.Arrays.Zones], 
       [DataBase.Nodes.Data,DataBase.Links.Data,DataBase.Turns.Data,DataBase.OrigConnectors.Data,DataBase.DestConnectors.Data,DataBase.Zones.Data], 
       [DataBase.Nodes.DictionaryPy2Vis,DataBase.Links.DictionaryPy2Vis,DataBase.Turns.DictionaryPy2Vis,DataBase.OrigConnectors.DictionaryPy2Vis,DataBase.DestConnectors.DictionaryPy2Vis,DataBase.Zones.DictionaryPy2Vis], 
       [DataBase.Nodes.DictionaryVis2Py,DataBase.Links.DictionaryVis2Py,DataBase.Turns.DictionaryVis2Py,DataBase.OrigConnectors.DictionaryVis2Py,DataBase.DestConnectors.DictionaryVis2Py,DataBase.Zones.DictionaryVis2Py], 
       [DataBase.Paths.List]],[Results.VersionName,Results.noZones,Results.noNodes,Results.noLinks,Results.noTurns,Results.noTrips, 
             Results.Times.VersionLoad,Results.Times.GetData,Results.Times.GetCoords,Results.Times.CrossTheTime,Results.Times.Plot_Cylinder, 
             Results.AssType,Results.AssParam,Results.tStart,Results.tEnd,Results.Distribution,Results.tVector]]= cPickle.load(plik)` 

錯誤的是(在這兩種情況下):

Traceback (most recent call last): 
    File "D:\programy\projekt_eclipse\src\Praca\wx_frame.py", line 342, in LoadDatabase_Handler 
    Load_DataBase() 
    File "D:\programy\projekt_eclipse\src\Praca\wx_frame.py", line 1804, in Load_DataBase 
    A= cPickle.load(plik) 
ImportError: No module named multiarray 

什麼想法?

PS。現在我已經解決了這個問題,部分地說:/我需要改變數組的格式。我試圖追查錯誤,但我不能。導致錯誤的變量是(long :)):

[[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 
    0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00] 
[ 1.00000000e+00 0.00000000e+00 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 0.00000000e+00] 
[ 2.00000000e+00 0.00000000e+00 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 3.52875186e+04] 
[ 3.00000000e+00 0.00000000e+00 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.24880978e+04] 
[ 4.00000000e+00 0.00000000e+00 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.24880978e+04] 
[ 5.00000000e+00 0.00000000e+00 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.24880978e+04] 
[ 6.00000000e+00 0.00000000e+00 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.24880978e+04] 
[ 7.00000000e+00 0.00000000e+00 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.24880978e+04] 
[ 8.00000000e+00 0.00000000e+00 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 3.59846476e+04] 
[ 9.00000000e+00 0.00000000e+00 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 0.00000000e+00] 
[ 1.00000000e+01 1.00000000e+03 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 3.97583022e+04] 
[ 1.10000000e+01 1.00000000e+03 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.84929461e+04] 
[ 1.20000000e+01 1.00000000e+03 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 8.76891311e+03] 
[ 1.30000000e+01 1.00000000e+03 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 5.10636164e+03] 
[ 1.40000000e+01 1.00000000e+03 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.45841100e+03] 
[ 1.50000000e+01 1.00000000e+03 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 4.22093915e+03] 
[ 1.60000000e+01 1.00000000e+03 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.20282091e+03] 
[ 1.70000000e+01 1.00000000e+03 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.86566159e+04] 
[ 1.80000000e+01 1.00000000e+03 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 3.80902598e+04] 
[ 1.90000000e+01 2.00000000e+03 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.23193676e+04] 
[ 2.00000000e+01 2.00000000e+03 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.16000116e+04] 
[ 2.10000000e+01 2.00000000e+03 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.05680012e+03] 
[ 2.20000000e+01 2.00000000e+03 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.89123867e+03] 
[ 2.30000000e+01 2.00000000e+03 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 4.98898168e+03] 
[ 2.40000000e+01 2.00000000e+03 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 7.44216130e+03] 
[ 2.50000000e+01 2.00000000e+03 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.23593332e+04] 
[ 2.60000000e+01 2.00000000e+03 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.14424233e+04] 
[ 2.70000000e+01 2.00000000e+03 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.91864355e+04] 
[ 2.80000000e+01 3.00000000e+03 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.07766798e+04] 
[ 2.90000000e+01 3.00000000e+03 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 8.61849685e+03] 
[ 3.00000000e+01 3.00000000e+03 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.09785208e+04] 
[ 3.10000000e+01 3.00000000e+03 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 8.99736773e+03] 
[ 3.20000000e+01 3.00000000e+03 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.06209122e+03] 
[ 3.30000000e+01 3.00000000e+03 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.48702707e+03] 
[ 3.40000000e+01 3.00000000e+03 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.04653099e+04] 
[ 3.50000000e+01 3.00000000e+03 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.25314801e+03] 
[ 3.60000000e+01 3.00000000e+03 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.67608539e+04] 
[ 3.70000000e+01 4.00000000e+03 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.07766798e+04] 
[ 3.80000000e+01 4.00000000e+03 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.82241178e+03] 
[ 3.90000000e+01 4.00000000e+03 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 8.05149043e+03] 
[ 4.00000000e+01 4.00000000e+03 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.55692239e+03] 
[ 4.10000000e+01 4.00000000e+03 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.19199226e+04] 
[ 4.20000000e+01 4.00000000e+03 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 8.43876335e+03] 
[ 4.30000000e+01 4.00000000e+03 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 4.90454231e+03] 
[ 4.40000000e+01 4.00000000e+03 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.03525083e+03] 
[ 4.50000000e+01 4.00000000e+03 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.67608539e+04] 
[ 4.60000000e+01 5.00000000e+03 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.07766798e+04] 
[ 4.70000000e+01 5.00000000e+03 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.07842319e+03] 
[ 4.80000000e+01 5.00000000e+03 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.48191278e+03] 
[ 4.90000000e+01 5.00000000e+03 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.06547361e+04] 
[ 5.00000000e+01 5.00000000e+03 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.27500595e+04] 
[ 5.10000000e+01 5.00000000e+03 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.62319628e+03] 
[ 5.20000000e+01 5.00000000e+03 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.50364667e+03] 
[ 5.30000000e+01 5.00000000e+03 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.48651846e+03] 
[ 5.40000000e+01 5.00000000e+03 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.67608539e+04] 
[ 5.50000000e+01 6.00000000e+03 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.16862400e+04] 
[ 5.60000000e+01 6.00000000e+03 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.88311307e+03] 
[ 5.70000000e+01 6.00000000e+03 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 7.89923519e+03] 
[ 5.80000000e+01 6.00000000e+03 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 8.16959736e+03] 
[ 5.90000000e+01 6.00000000e+03 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.49942081e+03] 
[ 6.00000000e+01 6.00000000e+03 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 6.24620368e+03] 
[ 6.10000000e+01 6.00000000e+03 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.27811830e+03] 
[ 6.20000000e+01 6.00000000e+03 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.13336356e+04] 
[ 6.30000000e+01 6.00000000e+03 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.91853045e+04] 
[ 6.40000000e+01 7.00000000e+03 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 3.67326624e+04] 
[ 6.50000000e+01 7.00000000e+03 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.79192625e+04] 
[ 6.60000000e+01 7.00000000e+03 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.35835049e+03] 
[ 6.70000000e+01 7.00000000e+03 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 4.66349011e+03] 
[ 6.80000000e+01 7.00000000e+03 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.88664273e+03] 
[ 6.90000000e+01 7.00000000e+03 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 4.15546726e+03] 
[ 7.00000000e+01 7.00000000e+03 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 9.26420582e+03] 
[ 7.10000000e+01 7.00000000e+03 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 1.80179725e+04] 
[ 7.20000000e+01 7.00000000e+03 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 3.69846102e+04] 
[ 7.30000000e+01 8.00000000e+03 0.00000000e+00 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 0.00000000e+00] 
[ 7.40000000e+01 8.00000000e+03 1.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 3.66207833e+04] 
[ 7.50000000e+01 8.00000000e+03 2.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.32529854e+04] 
[ 7.60000000e+01 8.00000000e+03 3.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.32529854e+04] 
[ 7.70000000e+01 8.00000000e+03 4.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.32529854e+04] 
[ 7.80000000e+01 8.00000000e+03 5.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.32529854e+04] 
[ 7.90000000e+01 8.00000000e+03 6.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 2.32529854e+04] 
[ 8.00000000e+01 8.00000000e+03 7.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 3.70098656e+04] 
[ 8.10000000e+01 8.00000000e+03 8.00000000e+03 2.00000000e+01 
    0.00000000e+00 5.00000000e+02 2.00000000e+01 0.00000000e+00]] 

cPickle或pickle無法加載它。但是當我用控制檯手動執行時,相同的文件結構([[]]和所有格式相同,值也是e + 00格式)然後它工作正常???????????我勒個去? 反正我已經解決了通過changign數據格式問題:/

+0

我打賭你使用Git? – kb0 2016-05-05 21:55:50

回答

1

您是否嘗試過進口多陣列明確? pickle需要定義所有的類才能導入數據。

+0

你的意思是: from numpy.core import multiarray as multiarray nope - 它沒有幫助。 – Rafal 2010-06-09 10:43:12

2

首先檢查是否$ YOUR_PYTHON_INSTALLATION/lib中/ Python的-x.x中/站點包/ numpy的/核心/ multiarray.so文件存在。

而且這將是,如果你發佈的全回溯,不僅錯誤信息是非常有用的。

+0

我檢查了它,它工作正常,並且文件存在, 從numpy.core導入多陣列作爲多陣列 我也重新安裝numpy它沒有幫助。我發佈完整的回溯問題 – Rafal 2010-06-09 15:58:09

0

能否請您發佈完整的回溯,因爲它可能是有用的,看到這個錯誤的來源。另外,你能否提供一個保存案例的追溯,我想知道它是在序列化期間還是在更早的地方發生。

9

我有一個Windows XP機器與守則在Linux下工作得很好了同樣的問題。它可能與文本和二進制文件的不同處理有關。當您的數據,試圖創建文件對象,明確指出你想二進制模式,即

plik=open(filename,'wb') 

代替

plik=open(filename,'w') 

爲我工作。

0

您必須使用一個非常古老的Python。因爲'import *'只在模塊級別可用。總之,要回答你的問題:

移動這些語句

import cPickle 
from scipy import * 
from numpy import * 

出Load_DataBase定義的,你會沒事的。引發異常是因爲cPickle無法找到plik內容的元信息。

1

這可以通過在Windows機器上autocrlf混帳改變行結束而引起的。您會注意到,直到您更改分支或執行其他任何刪除並重寫磁盤上的文件的操作,它纔會成爲問題。 此行添加到您的.gitattributes文件,以避免類似文本(但實際上二進制!)泡菜文件重寫行尾:

# .gitattributes 
# Pickle files are to be treated as binary. 
*.p binary 
*.lft binary