简体   繁体   English

在Python中使用cPickle导入错误

[英]Import Error using cPickle in Python

I am using Pickle in Python2.7. 我在Python2.7中使用Pickle。 I am getting error while using cPickle.load() method. 使用cPickle.load()方法时出现错误。 The code and error is shown below. 代码和错误如下所示。 Can someone guide me through this? 有人可以指导我吗?

Code: 码:

#! usr/bin/python
import cPickle
fo = open('result','rb')
dict1 = cPickle.load(fo)

Error: 错误:

Traceback (most recent call last):
File "C:\Python27\test.py", line 7, in <module>
dicts = cPickle.load(fo)
ImportError: No module named options

It seems like you can not do 看来你做不到

import options

but when you or someone else did 但是当你或别人做了

cpickle.dump(xxx, open('result', 'rb'))

there was an object with a class or function of a module options that existed at this point in time, in xxx . 此时存在一个带有模块options的类或功能的对象,在xxx

Solution

  1. You can open the file binarily and replace options with the module you replaced the old module options with. 您可以binarily打开该文件,并替换options与您更换旧模块的模块options用。

  2. You probably created the file in your package like in module package.main by executing the file main.py or something like it, having a module options in the same directory. 您可能是通过执行文件main.py或类似的文件在同一个目录中具有模块options ,从而在文件包中像在模块package.main那样创建了文件。 Now you do import package.main , try to read the file and options is now called package.options and the module options can not be found. 现在,您要import package.main ,尝试读取该文件,并且options现在称为package.options和模块options

  3. How did you create this file? 您是如何创建此文件的? How do you load it now? 您现在如何加载? cPickle/pickle does not transfer source code - so if you use a function you need the module when you load it. cPickle / pickle不会传输源代码-因此,如果使用功能,则在加载模块时需要该模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM