Development

How to install cPickle in Python 3

Captain Salem 1 min read

How to install cPickle in Python 3

The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object is converted back into an object hierarchy.

The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes.

The cPickle module implements the same algorithm in C instead of Python. It is often faster than the Python implementation but does not allow the user to subclass from Pickle.

In Python 3, you will fetch an error when importing the cPickle module. This brief post will show you a simple method of resolving such as errors.

Importing cPickle Module in Python 3

The cPickle module is included in the Standard Library of Python version 2. Hence, if you are in Python 3, you need to import the cPickle module as:

>>> import _pickle as cPickle

It is recommended to just use the pickle module that comes in the Standard Library of both Python 2 and Python 3.

You can also add error handling if you wish to support both Python versions as:

try:
  import cPickle as pickle
except:
  import pickle

End.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.