Import Error using cPickle in Python

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

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

  1. You can open the file binarily and replace options with the module you replaced the old module options with.

  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. 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.

  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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!