导入错误:没有名为 copy_reg pickle 的模块
我正在尝试取消存储在 MySQL 数据库中作为 blob 的对象。 我已经手动生成并存储了 pickled 对象在数据库中,但是当我尝试 unpickle 该对象时,我收到以下相当神秘的异常:
ImportError: No module named copy_reg
关于为什么会发生这种情况有什么想法吗?
重现方法
注意:必须在 Windows PC 上执行步骤 1,在 Linux PC 上执行步骤 3 和 4。
1) 在 Windows PC 上:
file = open("test.txt", "w")
thing = {'a': 1, 'b':2}
cPickle.dump(thing, file)
2) 手动将 text.txt 的内容插入到 Linux 上运行的 MySQL 数据库的 blob 字段中
3) 在 Linux 机器上运行的 Python 中,从 MySQL 中获取列的内容
4) 假设您将内容放入将 blob 列放入名为 data 的变量中,请尝试以下操作:
cPickle.loads(rawString)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
看来这可能是由我导出腌制对象的方法引起的。
此错误报告似乎表明我的问题可以通过导出到以二进制模式编写的文件来解决。 我现在要尝试一下,看看这是否能解决我的问题。
更新:这有效。 解决方案是确保将 pickle 对象导出到以二进制模式打开的文件中,即使您使用的是默认协议 0(通常称为“文本”)。
基于相关原始示例的正确代码:
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:
另外,简单地在(windows创建的)pickle文件上运行dos2unix(在linux下)就解决了我的问题。 (还没有尝试过开放模式“wb”。)
担
Also, simply running dos2unix (under linux) over the (windows-created) pickle file solved the problem for me. (Haven't tried the open mode 'wb' thing.)
Dan
只是一个交互式 python 会话来表明您不需要任何特定的代码来解决此问题:
在 Windows 计算机上执行类似的操作
,然后尝试从 Linux 机器中检索数据,
如果您这样做,错误消息可能会更加令人困惑只是酸洗碱类型。 这是我通过列表
[12, 1.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
and then try to retrieve the data from a linux box
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, '']
:正如另一个答案中提到的,使用
或使用如下所示的 tr 命令(删除回车符和 ctrl-z)
或使用
awk
(gawk
或nawk
如果是旧版本)或者如果您可以在 Linux 中重新创建 pickle 文件,请使用它。
As mentioned in the other answer use
OR use the tr command like below (removes carriage returns and ctrl-z)
OR use
awk
(gawk
ornawk
if its old versions)OR if you can recreate a pickle file in linux, use that.
这里发生的另一件事是,在将 pickle 转储到文件后,您似乎没有关闭该文件。 此处报告的错误有时可能是由于未关闭文件引起的(无论是在 Windows 计算机上还是其他计算机上)。
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.
我的问题:
当我从Windows获取model.pkl时,在我的Mac上运行此代码,这个问题即将
解决:
没问题!
my issue:
when i get the model.pkl from windows run this code in my mac,this issue is comming
solve:
is ok!
发生这种情况是因为在Windows中,新行符号存储为“\r\n”,而在Linux中是“\n”,所以你应该做的是逐行读取pickle文件,替换“\r\n” ”与“\n”,并将其写回到文件中。
与 dos2unix 方法会跳过二进制文件不同,这也适用于二进制 pickle 文件。
代码很简单:
此外,您可以使用正常写入模式,只需将“wb”更改为“w”和“rb” ”到“e”。
This happen because in Windows, new line symbol is stored as "\r\n", and that in Linux is "\n",so what you should do is to read the pickle file line by line, replace "\r\n" with "\n", and write it back to the file.
Unlike the dos2unix approach will skip binary file, this also works well for binary pickle file.
Code is simple:
Also, you can use normal write mode, just change "wb" to "w" and "rb" to "e".
我遇到了这个问题,因为 git 改变了
Windows 上的行结尾。
我通过将 .gitattributes 文件添加到我的存储库中解决了该问题:
另请参阅 https://github.com/actions/checkout/issues/135
I ran into this problem, because of git changing the
line endings on windows.
I solved the problem by adding a
.gitattributes
file to my repo:See also https://github.com/actions/checkout/issues/135
pickle 加载可能不会在与 python 脚本相同的位置查找。 有时目录会根据您的应用程序而变化。 在加载 pickle 之前,打印 os.getcwd() 以找出解决方案。
The pickle load may not be looking in the same location as your python script. Sometimes the directory changes on the basis of your application. Just before you load the pickle, print a os.getcwd() to work out a solution.