Python 3.7 Error: Unsupported Pickle Protocol 5

PythonPickle

Python Problem Overview


I'm trying to restore a pickled config file from RLLib (json didn't work as shown in this post), and getting the following error:

config = pickle.load(open(f"{path}/params.pkl", "rb"))

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-c964561b863c> in <module>
----> 1 config = pickle.load(open(f"{path}/params.pkl", "rb"))

ValueError: unsupported pickle protocol: 5

Python Version = 3.7.0

How can I open this file in 3.7?

Python Solutions


Solution 1 - Python

For pandas users who saved a dataframe to a pickle file with protocol 5 in python 3.8 and need to load it into python 3.6 which only supports protocol 4 (I'm looking at you google colab):

!pip3 install pickle5
import pickle5 as pickle
with open(path_to_protocol5, "rb") as fh:
  data = pickle.load(fh)

Could also save into a protocol-4 pickle from python 3.6

data.to_pickle(path_to_protocol4)

Update: If facing this when loading a model from stable-baselines3:

!pip install --upgrade --quiet cloudpickle pickle5
from stable_baselines3 import PPO
# restart kernel if in jupyter notebook

# Might not need this dict in all cases
custom_objects = {
    "lr_schedule": lambda x: .003,
    "clip_range": lambda x: .02
}
model = PPO.load("path/to/model.zip", custom_objects=custom_objects)

Tested on 2021-05-31 with env:

cloudpickle: 1.6.0
pickle5: 0.0.11 
stable-baselines3: 1.0

Reference: https://brainsteam.co.uk/2021/01/14/pickle-5-madness-with-mlflow/

Solution 2 - Python

Use pickle5 or load it into python 3.8+ and then serialize it to a lower version of it using the protocol parameter.

Solution 3 - Python

In the event that you cannot load pickle5 because of its dependencies (mainly Visual Building c++), another solution could be that you change the Python interpreter you're using (to the old one, before the error occured). For me, I was getting this error after I ran a program in IDLE that I had been running in Spyder. When I ran it again within Spyder, it dropped this error.

Python Error: Unsupported Pickle Protocol 5 

To resolve this, within Spyder I changed my Python interpreter to the Python I was using with IDLE (Tools -> Preferences). Once I rebooted Spyder, I had to install the necessary dependencies with command prompt so that within Spyder the console could be used:

pip install spyder-kernels

Naturally, this may introduce some irregularities within Spyder (namely, packages it's supposed to come with are no longer there because of the different interpreter). These should easily be sussed out when debugging, and resolved using standard pip installs.

Once you recover your (thought to be lost) files, it might be wise to think about reverting back to Spyder's Python interpreter, and updating code to elegantly handle this problem (I would love if somebody could suggest in comments how to do this that didn't require pickle5!)

Solution 4 - Python

If this error is due to heroku deployment then check your python version of local setup and heroku setup. If both are different then it might lead you to this error.
Solution:

  • Create a runtime.txt file in your application's base directory

Then inside your runtime.txt mention your local python version so that there are no conflicts.

python-3.9.2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhubbs5View Question on Stackoverflow
Solution 1 - PythonShadiView Answer on Stackoverflow
Solution 2 - Pythonhd1View Answer on Stackoverflow
Solution 3 - Pythonntk4View Answer on Stackoverflow
Solution 4 - Pythonsauravjoshi23View Answer on Stackoverflow