paragk wrote: > 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 > > > [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()
[...] > From the above analysis, clearly the collection module exists, since > the dump works. > > What am I missing? You have to open the file in binary mode "wb". >>> import collections >>> import pickle >>> data = pickle.dumps(collections.defaultdict()) >>> pickle.loads(data) defaultdict(None, {}) Now simulate the effect of writing in text mode and reading in binary mode: >>> garbled_data = data.replace("\n", "\r\n") >>> pickle.loads(garbled_data) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.6/pickle.py", line 1374, in loads return Unpickler(file).load() File "/usr/lib/python2.6/pickle.py", line 858, in load dispatch[key](self) File "/usr/lib/python2.6/pickle.py", line 1090, in load_global klass = self.find_class(module, name) File "/usr/lib/python2.6/pickle.py", line 1124, in find_class __import__(module) ImportError: No module named collections >>> try: pickle.loads(garbled_data) ... except ImportError as e: ... e ... ImportError('No module named collections\r',) So as Steven suspected there was a whitespace char in the module name: pickle.load() was looking for the "collections\r" module. Peter -- http://mail.python.org/mailman/listinfo/python-list