Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named copy_reg pickle

Tags:

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, I get the following rather cryptic exception:

ImportError: No module named copy_reg

Any ideas as to why this happens?

Method of Reproduction

Note: Must do step 1 on a Windows PC and steps 3 and 4 on a Linux PC.

1) On a Windows PC:

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

2) Manually insert contents of text.txt into blob field of MySQL database running on linux

3) In Python running on a linux machine, fetch the contents of column from MySQL

4) Assuming that you put the contents of the blob column into a variable called data, try this:

cPickle.loads(rawString) 
like image 347
Stephen Edmonds Avatar asked Feb 17 '09 10:02

Stephen Edmonds


1 Answers

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) 
like image 131
Stephen Edmonds Avatar answered Sep 20 '22 13:09

Stephen Edmonds