可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using Pickle in Python2.7. I am getting error while using cPickle.load()
method. 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
回答1:
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
.
Solution
You can open the file binarily and replace options
with the module you replaced the old module options
with.
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. 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.
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.