Questions tagged [pickle]

Pickle is an object serialization module for Python. Use this tag together with the Python tag for questions related to storing or loading objects with Pickle.

Pickle provides a powerful set of tools for serializing and de-serializing Python objects.

4936 questions
146
votes
4 answers

Simple example of use of __setstate__ and __getstate__

I don't know what the __setstate__ and __getstate__ methods do, so help me with a simple example.
zjm1126
  • 63,397
  • 81
  • 173
  • 221
145
votes
2 answers

Unpickling a python 2 object with python 3

I'm wondering if there is a way to load an object that was pickled in Python 2.4, with Python 3.4. I've been running 2to3 on a large amount of company legacy code to get it up to date. Having done this, when running the file I get the following…
NDevox
  • 4,056
  • 4
  • 21
  • 36
144
votes
9 answers

Common use-cases for pickle in Python

I've looked at the pickle documentation, but I don't understand where pickle is useful. What are some common use-cases for pickle?
satoru
  • 31,822
  • 31
  • 91
  • 141
139
votes
1 answer

Preferred (or most common) file extension for a Python pickle

At times, I've seen .pickle, .pck, .pcl, and .db for files that contain Python pickles, but I am unsure what is the most common or best practice. I know that the latter three extensions are also used for other things. The related question is: What…
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
138
votes
4 answers

What are the different use cases of joblib versus pickle?

Background: I'm just getting started with scikit-learn, and read at the bottom of the page about joblib, versus pickle. it may be more interesting to use joblib’s replacement of pickle (joblib.dump & joblib.load), which is more efficient on big…
msunbot
  • 1,871
  • 4
  • 16
  • 16
137
votes
12 answers

Is there an easy way to pickle a python function (or otherwise serialize its code)?

I'm trying to transfer a function across a network connection (using asyncore). Is there an easy way to serialize a python function (one that, in this case at least, will have no side effects) for transfer like this? I would ideally like to have a…
Michael Fairley
  • 12,980
  • 4
  • 26
  • 23
134
votes
5 answers

Python pickle error: UnicodeDecodeError

I'm trying to do some text classification using Textblob. I'm first training the model and serializing it using pickle as shown below. import pickle from textblob.classifiers import NaiveBayesClassifier with open('sample.csv', 'r') as fp: cl…
90abyss
  • 7,037
  • 19
  • 63
  • 94
128
votes
4 answers

How to unpack pkl file?

I have a pkl file from MNIST dataset, which consists of handwritten digit images. I'd like to take a look at each of those digit images, so I need to unpack the pkl file, except I can't find out how. Is there a way to unpack/unzip pkl file?
ytrewq
  • 3,670
  • 9
  • 42
  • 71
124
votes
2 answers

Cannot load pickled object

The problem I am having is when I try to load the pickled object. I have tried using both pickle.loads and pickle.load Here are the results: pickle.loads: TypeError: 'str' does not support the buffer interface pickle.load: TypeError: file must…
Houdini
  • 3,442
  • 9
  • 31
  • 47
112
votes
4 answers

What does it mean for an object to be picklable (or pickle-able)?

Python docs mention this word a lot and I want to know what it means.
Paul
  • 2,115
  • 3
  • 17
  • 14
107
votes
2 answers

ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?

I use pickle to dump a file on python 3, and I use pickle to load the file on python 2, the ValueError appears. So, python 2 pickle can not load the file dumped by python 3 pickle? If I want it? How to do?
Aleeee
  • 1,913
  • 6
  • 17
  • 27
99
votes
4 answers

Python serialization - Why pickle?

I understood that Python pickling is a way to 'store' a Python Object in a way that does respect Object programming - different from an output written in txt file or DB. Do you have more details or references on the following points: where are…
kiriloff
  • 25,609
  • 37
  • 148
  • 229
99
votes
8 answers

Saving and loading multiple objects in pickle file?

I have a class that serves players in a game, creates them and other things. I need to save these player objects in a file to use it later. I've tried the pickle module but I don't know how to save multiple objects and again loading them? Is there…
hamidfzm
  • 4,595
  • 8
  • 48
  • 80
97
votes
3 answers

What is the difference between pickle and shelve?

I am learning about object serialization for the first time. I tried reading and 'googling' for differences in the modules pickle and shelve but I am not sure I understand it. When to use which one? Pickle can turn every python object into stream…
zubinmehta
  • 4,368
  • 7
  • 33
  • 51
95
votes
7 answers

Python pickling after changing a module's directory

I've recently changed my program's directory layout: before, I had all my modules inside the "main" folder. Now, I've moved them into a directory named after the program, and placed an __init__.py there to make a package. Now I have a single .py…
Steven Sproat
  • 4,398
  • 4
  • 27
  • 40