即使导入了pickle,调用pickle也会导致load()和dump()上出现AttributeError

时间:2018-02-11 20:33:21

标签: python intellij-idea

这应该非常简单:我过去经常使用pickle。这次出了什么问题?

import pickle

with open('data/cifar-10-batches-py/data_batch_1','rb') as f:
    dat = pickle.load(f)

AttributeError: 'function' object has no attribute 'load'

以下内容也失败(如果load被注释掉了)

with open ('data/cifar-10-batches-py/small_batch','wb') as f:
    pickle.dump(images[0:100],f, protocol=pickle.HIGHEST_PROTOCOL)

AttributeError: 'function' object has no attribute 'dump'

所以似乎pickle没有被正确导入?

这是python 2.7.3

1 个答案:

答案 0 :(得分:0)

pickle被以下内容遮蔽(我未在OP中加入,因为我不知道它是否相关

from cifar import *

它有

# pickle object
# obj: object to pickle
# file_path: file path to store object at in binary cpython blob
def pickle(obj, file_path):
    with open(file_path, "wb") as fo:
        cPickle.dump(obj, fo)

值得怀疑的是,cifar以这种方式创建了一个名为pickle的方法。所以,我需要更加谨慎地做

 from some_library import *