Last updated: Apr 11, 2024
Reading timeยท4 min
pickle5
module to solve the errorThe Python "ValueError: unsupported pickle protocol: 5" occurs when you try to deserialize an object using a Python version that doesn't support Pickle protocol 5, e.g. Python 3.7 or older.
You can solve the error in multiple ways:
pickle5
module.Here is an example of how the error occurs.
import pickle employee = { 'first': 'bobby', 'last': 'hadz', 'salary': '1500' } file_path = 'employees.txt' # ๐๏ธ Pickled using Python 3.8 with open(file_path, 'wb') as file_obj: pickle.dump(employee, file_obj) # ๐๏ธ Unpickled using Python 3.7 # โ๏ธ ValueError: unsupported pickle protocol: 5 with open(file_path, 'rb') as file_obj: unpickler = pickle.Unpickler(file_obj) employee_data = unpickler.load() print(employee_data)
We pickled (serialized) the object using Python 3.8 and tried to unpickle (deserialize it) using Python 3.7.
If you need to check your Python version, use the python --version
command.
python --version
If your Python interpreter runs a version of 3.7 or older, you can't run the Pickle 5 protocol.
pickle5
module to solve the errorOne way to solve the error is to use the pickle5 module.
The pickle5
module backports all features and APIs that were added in the
pickle
module in Python 3.8.3.
The module works with Python 3.5, 3.5 and 3.7.
Open your terminal in your project's root directory and run the following command.
pip install pickle5 pip3 install pickle5
If you use Jupyter Notebook (or Google Colab), prefix the command with an exclamation mark.
!pip3 install pickle5
Now import and use the pickle5
module as follows.
import pickle5 as pickle employee = { 'first': 'bobby', 'last': 'hadz', 'salary': '1500' } file_path = 'employees.txt' # ๐๏ธ Pickled using Python 3.8 with open(file_path, 'wb') as file_obj: pickle.dump(employee, file_obj) # ๐๏ธ Unpickled using Python 3.7 with open(file_path, 'rb') as file_obj: unpickler = pickle.Unpickler(file_obj) employee_data = unpickler.load() print(employee_data)
All you have to do is replace your import statement.
In other words, replace this:
# โ๏ธ Before import pickle
With this:
# โ After import pickle5 as pickle
If you run your code in Google Colab and are stuck with Python version 3.7, the
pickle5
module should be your preferred approach.
Alternatively, you can use a lower Pickle protocol when serializing the object to solve the error.
For example, you can set the protocol
keyword argument to 4
when calling
pickle.dump()
or pickle.dumps()
.
import pickle employee = { 'first': 'bobby', 'last': 'hadz', 'salary': '1500' } file_path = 'employees.txt' with open(file_path, 'wb') as file_obj: # ๐๏ธ Set pickle protocol to 4 pickle.dump(employee, file_obj, protocol=4) # ๐๏ธ Unpickled using Python 3.7 with open(file_path, 'rb') as file_obj: unpickler = pickle.Unpickler(file_obj) employee_data = unpickler.load() print(employee_data)
We open the file in wb
(write binary) mode and use the
pickle.dump()
method to write the pickled representation of the dictionary to the file object.
Notice that we set the protocol
keyword argument to 4
.
We are then able to use an older Python version when deserializing (unpickling) the file object.
5
was added in Python 3.8.4
was added in Python 3.4.3
was added in Python 3.0.2
was added in Python 2.3.If you pickle an object using Python 3 and need to unpickle it in Python 2, you
have to set the protocol
to 2
.
import pickle employee = { 'first': 'bobby', 'last': 'hadz', 'salary': '1500' } file_path = 'employees.txt' with open(file_path, 'wb') as file_obj: # ๐๏ธ Set pickle protocol to 2 pickle.dump(employee, file_obj, protocol=2) # ๐๏ธ Unpickled using Python 2.7 with open(file_path, 'rb') as file_obj: unpickler = pickle.Unpickler(file_obj) employee_data = unpickler.load() print(employee_data)
We pickle the object in Python 2 but set the protocol
to version 2
.
We can then unpickle the object using Python 2 because the protocol
was set to
2
.
If none of the suggestions helped, you can install a more recent version of Python (3.8+).
You can download the Python installer from
the official python.org
website.
Once you've installed the latest source release version, check your Python
version with python --version
and rerun the Python script.
python --version
Pickle protocol 5 was introduced in Python version 3.8.3, so as long as your Python version is 3.8+, you should be good to go.
You can learn more about the related topics by checking out the following tutorials: