ValueError: unsupported pickle protocol: 5 [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. ValueError: unsupported pickle protocol: 5
  2. Using the pickle5 module to solve the error
  3. Use a lower Pickle protocol to solve the error
  4. Install Python version 3.8 or later

# ValueError: unsupported pickle protocol: 5 [Solved]

The 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:

  1. Installing and using the pickle5 module.
  2. Upgrading your Python version to 3.8 or more recent.
  3. Serializing the object with a lower protocol version (e.g. version 4).

Here is an example of how the error occurs.

main.py
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.

The Pickle 5 protocol was introduced in Python version 3.8 and is not supported in older Python versions.

If you need to check your Python version, use the python --version command.

shell
python --version

get python version

If your Python interpreter runs a version of 3.7 or older, you can't run the Pickle 5 protocol.

# Using the pickle5 module to solve the error

One 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.

shell
pip install pickle5 pip3 install pickle5

install pickle5 module

If you use Jupyter Notebook (or Google Colab), prefix the command with an exclamation mark.

shell
!pip3 install pickle5

Now import and use the pickle5 module as follows.

main.py
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:

main.py
# โ›”๏ธ Before import pickle

With this:

main.py
# โœ… After import pickle5 as pickle

using pickle5 module to solve the error

If you run your code in Google Colab and are stuck with Python version 3.7, the pickle5 module should be your preferred approach.

# Use a lower Pickle protocol to solve the error

Alternatively, you can use a lower Pickle protocol when serializing the object to solve the error.

You have to run the following script with Python 3.8+ to be able to lower the Pickle protocol.

For example, you can set the protocol keyword argument to 4 when calling pickle.dump() or pickle.dumps().

main.py
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)

set lower pickle protocol

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.

  • The Pickle protocol version 5 was added in Python 3.8.
  • The protocol version 4 was added in Python 3.4.
  • The protocol version 3 was added in Python 3.0.
  • The protocol version 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.

main.py
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)

set pickle protocol to 2

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.

# Install Python version 3.8 or later

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.

shell
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev