manuersuper
manuersuper

Reputation: 145

ImportError: No module named x while loading a pickle file

I'm trying to unpickle a file, but I get a module not found error. The .pkl was generated with an old version of Python (don't know which one), but I need to use it in Python 3.11, so I'm trying to unpickle it and save in another format.

script.py

import pickle
with open("file.pkl", "rb") as f:
    data=pickle.load(f)

I get this error when I run it:

Traceback (most recent call last):
  File "C:\Users\myname\Desktop\file.py", line 5, in <module>
    data = pickle.load(f)
  File "C:\Python27\lib\pickle.py", line 1388, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 865, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 1097, in load_global
    klass = self.find_class(module, name)
  File "C:\Python27\lib\pickle.py", line 1134, in find_class
    __import__(module)
ImportError: No module named copy_reg

If I add 'import copy_reg' for example below import pickle, the script can correctly import it, but then script fails when the code reaches the pickle.load() line.

This is the relevant part of code in the pickle module:

pickle.py

def find_class(self, module, name):
    __import__(module) #this line gives error
    mod = sys.modules[module]
    klass = getattr(mod, name)
return klass

Doing some tests, I was able to see that no module can be imported (not even builtin module).

These are the first lines of the pickle file

file.PKL

ccopy_reg
_reconstructor
p0
...

I'm using Python 2.7.

Upvotes: -1

Views: 146

Answers (0)

Related Questions