Search code examples
python-3.xpicklepython-importlib

How do I use importlib.resources with pickle files?


I'm trying to load a pickle file with importlib.resources, but I'm getting the following error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

The bit that is raising the error is:

with importlib.resources.open_text("directory_with_pickle_file", "pickle_file.pkl") as f:
   data = pickle.load(f)

I'm certain that the file (pickle_file.pkl) was created with pickle.dump. What am I doing wrong?


Solution

  • Through lots of trial and error I figured out that importlib.resources has a read_binary function which can be used to read pickled files like so:

    text = importlib.resources.read_binary("directory_with_pickle_file", "pickle_file.pkl")
    data = pickle.loads(text)
    

    Here, data is the pickled object.