How do I know which versions of pickle a particular version of Python supports?

12,761

Like so:

>>> import pickle
>>> pickle.compatible_formats
['1.0', '1.1', '1.2', '1.3', '2.0']

Edit

I think it's safe to rely on the latest documentation. For example the pickle documentation for Python 3.2.1 states:

There are currently 4 different protocols which can be used for pickling.

  • Protocol version 0 is the original human-readable protocol and is backwards compatible with earlier versions of Python.

  • Protocol version 1 is the old binary format which is also compatible with earlier versions of Python.

  • Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes.

  • Protocol version 3 was added in Python 3.0. It has explicit support for bytes and cannot be unpickled by Python 2.x pickle modules. This
    is the current recommended protocol, use it whenever it is possible.

I think that makes it easy to confirm!

To explicitly answer your question, this means Python 2.6-2.7 support Pickle versions <= 2.0, and Python 3.0-3.2 support Pickle versions <= 3.0.

Share:
12,761
Paul Hoffman
Author by

Paul Hoffman

Updated on June 28, 2022

Comments

  • Paul Hoffman
    Paul Hoffman about 2 years

    I have a script that requires Python 2.6. I will be adding a large-ish pickled database to the next version, and I want to use the fastest version of pickling. How can I tell which versions of pickling are available in every version of Python 2.6 and later?

  • Paul Hoffman
    Paul Hoffman almost 13 years
    That tells me the versions of pickle in my current runtime. I want to know the version of pickle available to anyone running any version of 2.6 or later.
  • wjandrea
    wjandrea over 5 years
    There are now 5 protocols, with version 4 added in Python 3.4. See docs.python.org/3/library/pickle.html#data-stream-format