问题
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
optionswith the module you replaced the old moduleoptionswith.You probably created the file in your package like in module
package.mainby executing the filemain.pyor something like it, having a moduleoptionsin the same directory. Now you doimport package.main, try to read the file andoptionsis now calledpackage.optionsand the moduleoptionscan 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.
来源:https://stackoverflow.com/questions/20139583/import-error-using-cpickle-in-python