Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7 pickle won't recognize numpy multiarray

I need to load a set of pickled data from a collaborator. Problem is, it seems I need multiarray for this. My code is as below:

f = open('data.p', 'rb')
a = pickle.load(f)

And here is the error message.

ImportError                               Traceback (most recent call last)
<ipython-input-3-17918c47ae2d> in <module>()
----> 1 a = pk.load(f)

/usr/lib/python2.7/pickle.pyc in load(file)
   1382 
   1383 def load(file):
-> 1384     return Unpickler(file).load()
   1385 
   1386 def loads(str):

/usr/lib/python2.7/pickle.pyc in load(self)
    862             while 1:
    863                 key = read(1)
--> 864                 dispatch[key](self)
    865         except _Stop, stopinst:
    866             return stopinst.value

/usr/lib/python2.7/pickle.pyc in load_global(self)
   1094         module = self.readline()[:-1]
   1095         name = self.readline()[:-1]
-> 1096         klass = self.find_class(module, name)
   1097         self.append(klass)
   1098     dispatch[GLOBAL] = load_global

/usr/lib/python2.7/pickle.pyc in find_class(self, module, name)
   1128     def find_class(self, module, name):
   1129         # Subclasses may override this
-> 1130         __import__(module)
   1131         mod = sys.modules[module]
   1132         klass = getattr(mod, name)

ImportError: No module named multiarray

I thought it was the problem of the compiled numpy in my computer. So I uninstalled the numpy from my Arch Linux repo and installed the numpy through

sudo -H pip2 install numpy

Yet the problem persist. I have checked the folder $PACKAGE-SITE/numpy/core, multiarray.so is in it. And I have no idea why pickle can't load the module.

How can I solve the problem? What else do I need to do?

PS1. I am using Arch Linux. And tried all versions of python 2.7 since last year October. None of them works. PS2. Since the problem is with the loading step. I suspect the problem being more likely from internal conflicts of python rather than from the data file.

like image 656
Chong Avatar asked Dec 25 '22 09:12

Chong


1 Answers

Thanks to @MikeMcKems, the problem is now solved.

The issue is caused by different special symbols used MS Windows and Linux(eg. end of line symbol). My collaborator was using Windows machine, and saved the data with

pickle.dump(obj, 'filename', 'w')

The data was saved in plain text with a lot of special symbols in it. And when I load the data with my Linux machine, the symbols were misintepreted hence causing the problem.

The easiest way to solve it is to find a Windows machine, load the data with

a=pickle.load(open('filename_in', 'r'))

Then output with binary form

pickle.dump(a, open('filename_out', 'wb'))

Since binary data is universally recognized as long as you use pickle to read it, the file filename_out is easily recognizable by Python in linux.

like image 90
Chong Avatar answered Jan 15 '23 13:01

Chong