ImportError with pickle (Python 2.7.9), possibly platform dependent

Ben Sizer kylotan at gmail.com
Wed Apr 29 12:01:49 EDT 2015


I'm saving some data via pickle, and loading it in is proving tricky. 

Traceback (most recent call last):
  [...some lines removed...]
File "/home/kylotan/OMDBSetup.py", line 44, in get_omdb_map
  __omdb_map = OMDBMap.OMDBMap.load_from_binary(full_path)
File "/home/kylotan/OMDBMap.py", line 87, in load_from_binary
  d = pickle.load(binary_file)
File "/usr/local/lib/python2.7/pickle.py", line 1378, in load
  return Unpickler(file).load()
File "/usr/local/lib/python2.7/pickle.py", line 858, in load
  dispatch[key](self)
File "/usr/local/lib/python2.7/pickle.py", line 1090, in load_global
  klass = self.find_class(module, name)
File "/usr/local/lib/python2.7/pickle.py", line 1124, in find_class
  __import__(module)
ImportError: No module named OMDBMap

Here are the 2 weird things:

1) There clearly is a module named OMDBMap, and it's importable - it's there in the 2nd line of the traceback.
2) This error only arises on Linux. Exactly the same file loads in properly on MacOSX, and on Windows 8.


What I've done to try and debug this:

a) I've run an MD5 on the file to make sure the file is identical on all platforms, and that nothing is changing the line endings, and I'm also making sure to both open and save the pickle with 'rb'/'wb'.
b) I tried both pickle and cPickle - they seem to produce slightly different output but the error is exactly the same in each case.
c) I pickle and unpickle from exactly the same file (a file called OMDBSetup.py does 'import OMDBMap' and then calls methods in there to save and load the data (including the OMDBMap.OMDBMap.load_from_binary which contains the above callstack). The intention here was to avoid both the common "No module named __main__" error, and to hopefully have exactly the same modules imported into the namespace at both save and load time.

So my hypothesis is that I've either found some edge case which only acts weird on Linux (or only succeeds on the other platforms, whichever way you look at it), or there's something wrong with the Linux configuration that means it somehow cannot find this module (despite it already having found it to get this far).

Does anybody have any suggestions on how I can go about debugging this? Or refactoring it to avoid whatever is happening here?

-- 
Ben Sizer



More information about the Python-list mailing list