P
paragk
Hi,
I am unable to figure out the cause of python pickle unable to find
the collection module.
I am getting
__import__(module)
ImportError: No module named collections
when I try to load a pickled object.
Details:
Python version:
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit
(Intel)] on win32
I am unable to figure out the cause of python pickle unable to find
the collection module.
I am getting
__import__(module)
ImportError: No module named collections
when I try to load a pickled object.
Details:
Python version:
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit
(Intel)] on win32
Code:
import sys
import glob
import collections
import pickle
....
SomeListDict = collections.defaultdict(list)
.... # Populate SomeListDict
DictFile = open (options.saveDict, 'w')
pickle.dump(SomeListDict , DictFile, -1)
DictFile.close()
....
# Try loading the dictionary
LoadDictFile = open(options.loadDict, 'rb')
SomeListDict = pickle.load(LoadDictFile)
LoadDictFile.close()
....
[CODE END]
The pickle.dump(CallGraphDict, DictFile, -1) works fine. The
dictionary is saved.
The interesting thing is, the dump function is able to find the
collections module.
try:
__import__(module)
mod = sys.modules[module]
klass = getattr(mod, name)
The above try block succeeds.
module
str: collections
mod
module: <module 'collections' from 'C:\Python26\Lib\collections.pyc'>
klass
type: <type 'collections.defaultdict'>
The SomeListDict = pickle.load(LoadDictFile) fails with
__import__(module)
ImportError: No module named collections
The sequence of calls is:
def load_global(self):
module = self.readline()[:-1]
name = self.readline()[:-1]
klass = self.find_class(module, name)
self.append(klass)
dispatch[GLOBAL] = load_global
Variables:
module
str: collections
name
str: defaultdict
The code where the exception occurs is:
def find_class(self, module, name):
# Subclasses may override this
__import__(module)
mod = sys.modules[module]
klass = getattr(mod, name)
return klass
From the above analysis, clearly the collection module exists, since
the dump works.
What am I missing?