我正在尝试在Python3.6中加载(复制的)pickle对象,但是当我这样做时,会出现__builtin__\r
的导入错误。
with open('FilePath/FileName.pkl', "rb") as file:
file = pickle.load(file)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-36-b39fbab9bab4> in <module>()
1 with open('FilePath/FileName.pkl', "rb") as file:
----> 2 file = pickle.load(file)
ModuleNotFoundError: No module named '__builtin__\r'
在Python 2.7中尝试相同操作时,我再次遇到了一个导入错误,针对的是__builtin__
模块。
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-3-12ac6524bf1a> in <module>()
1 with open('FilePath/FileName.pkl', "rb") as file:
----> 2 file = pickle.load(file)
/Users/manasa.bulusu/anaconda2/lib/python2.7/pickle.pyc in load(file)
1382
1383 def load(file):
-> 1384 return Unpickler(file).load()
1385
1386 def loads(str):
/Users/manasa.bulusu/anaconda2/lib/python2.7/pickle.pyc in load(self)
862 while 1:
863 key = read(1)
--> 864 dispatch[key](self)
865 except _Stop, stopinst:
866 return stopinst.value
/Users/manasa.bulusu/anaconda2/lib/python2.7/pickle.pyc in load_global(self)
1094 module = self.readline()[:-1]
1095 name = self.readline()[:-1]
-> 1096 klass = self.find_class(module, name)
1097 self.append(klass)
1098 dispatch[GLOBAL] = load_global
/Users/manasa.bulusu/anaconda2/lib/python2.7/pickle.pyc in find_class(self, module, name)
1128 def find_class(self, module, name):
1129 # Subclasses may override this
-> 1130 __import__(module)
1131 mod = sys.modules[module]
1132 klass = getattr(mod, name)
ImportError: No module named __builtin__
我导入了builtins
(py3.6)和__builtin__
(py2.7),但仍然遇到相同的错误。
问题出在哪里?此外,在py36中,__builtin__\r
甚至看起来都不像一个真正的包。
\r
,通常是由于\n
转换为\r\n
引起的。 - user2357112'\r\n'
替换为'\n'
,希望能解决问题,但这不是一个好的长期计划;最好避免在第一次生成pickle文件时就出现这个问题。 - user2357112\r
,只是看不见而已。在Windows上用Python 2以文本模式打开它,你应该能够加载它。 - user2357112