Skip to content Skip to sidebar Skip to footer

Importerror: No Module Named Copy_reg Pickle

I'm trying to unpickle an object stored as a blob in a MySQL database. I've manually generated and stored the pickled object in the database, but when I try to unpickle the object,

Solution 1:

It seems this might be caused by my method of exporting the pickled object.

This bug report seens to suggest that my issue can be resolved by exporting to a file writen in binary mode. I'm going to give this a go now and see if this solves my issue.

UPDATE: This works. The solution is to make sure you export your pickled object to a file open in binary mode, even if you are using the default protocol 0 (commonly referred to as being "text")

Correct code based on orignal example in question:

file = open("test.txt", 'wb')
thing = {'a': 1, 'b':2}
cPickle.dump(thing, file)

Solution 2:

just an interactive python session to show that you don't need any particular code to have this problem:

do something like this on a windows machine

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32Type"help", "copyright", "credits"or"license"for more information.
>>> import pickle, re
>>> empty_string = re.compile("^$")
>>> pickle.dump([empty_string,1,1.23,'abc'], file('m:/mario/test-b.dump','wb'))
>>> pickle.dump([empty_string,1,1.23,'abc'], file('m:/mario/test-t.dump','wt'))
>>> 

and then try to retrieve the data from a linux box

Python 2.6.2 (release26-maint, Apr 192009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits"or"license"for more information.
>>> import pickle
>>> pickle.load(file('/home/mario/.gvfs/transfer on 192.168.0.4/mario/test-b.dump'))
/usr/lib/python2.6/pickle.py:1124: DeprecationWarning: The sre module is deprecated, please import re.
  __import__(module)
[<_sre.SRE_Pattern object at 0xb7d42420>, 1, 1.23, 'abc']
>>> pickle.load(file('/home/mario/.gvfs/transfer on 192.168.0.4/mario/test-t.dump'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/pickle.py", line 1370, inloadreturn Unpickler(file).load()
  File "/usr/lib/python2.6/pickle.py", line 858, inload
    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 sre
>>> 

the error message can be even more confusing if you are just pickling base types. this is what I get with the list [12, 1.2, '']:

ValueError: insecure string pickle

Solution 3:

As mentioned in the other answer use

dos2unix originalPickle.file outputPickle.file

OR use the tr command like below (removes carriage returns and ctrl-z)

tr -d '\15\32' < originalPickle.file > outputPickle.file

OR use awk (gawk or nawk if its old versions)

  awk '{ sub("\r$", ""); print }' originalPickle.file > outputPickle.file

OR if you can recreate a pickle file in linux, use that.

Solution 4:

Another thing going on here is that you don't seem to have closed the file after dumping the pickle to it. the error reported here can sometimes be caused (whether on a windows machine or otherwise) by not closing the file.

Solution 5:

my issue:

withopen('model.pkl', 'rb') as f:
    subsection_struct_model = pickle.load(f)

when i get the model.pkl from windows run this code in my mac,this issue is comming

solve:

dos2unix model.pkl 

is ok!

Post a Comment for "Importerror: No Module Named Copy_reg Pickle"