Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Python

How to Save and Load Python Objects with pickle

Learn how to use Python's built-in pickle module to serialize and deserialize complex objects for caching, model persistence, and state management. Includes best practices, security warnings, and when to use JSON instead.

July 2026 6 min read 1 views 0 hearts

Saving and Loading Python Objects with pickle

Have you ever written a Python script that builds up complex data structures—lists of dictionaries, trained machine learning models, or carefully configured objects—only to lose everything when the script finishes? I've been there too many times. That's where pickle comes in, and honestly, it's one of the most underrated tools in Python's standard library.

What Exactly is pickle?

pickle is Python's built-in module for object serialization. "Serialization" is just a fancy word for converting a Python object into a byte stream that you can save to a file or send over a network. When you need that object back, pickle deserializes the byte stream back into a living, breathing Python object.

Think of it like taking a snapshot of your object at a moment in time, storing that snapshot, and being able to reconstruct the exact same object later—even in a completely different Python session.

The Simple Recipe

Using pickle is surprisingly straightforward. Here's the basic pattern:

import pickle

# Your complex object
data = {
    'users': ['alice', 'bob', 'charlie'],
    'scores': [98, 76, 88],
    'active': True
}

# Save it
with open('data.pkl', 'wb') as file:
    pickle.dump(data, file)

# Load it back
with open('data.pkl', 'rb') as file:
    restored_data = pickle.load(file)

print(restored_data['users'])  # ['alice', 'bob', 'charlie']

Notice the 'wb' and 'rb' modes. You're working with binary files, not text—something that trips up beginners all the time at PythonSkillset.

What Can You Pickle?

This is where pickle really shines. You can serialize just about any Python object:

  • All native types: integers, strings, lists, dictionaries, tuples, sets, booleans
  • Class instances (provided the class definition is available when loading)
  • Functions and classes (by reference, not their actual code)
  • Nested combinations of all these

Let me give you a real-world example from our PythonSkillset community. A developer was building a recommendation engine that crunched user data for hours. Without pickle, they'd have to recompute everything every time the script restarted. With pickle, they saved the trained model state once and loaded it in seconds:

class RecommendationModel:
    def __init__(self):
        self.user_profiles = {}
        self.training_complete = False

    def train(self, data):
        # Imagine hours of computation here
        self.user_profiles = {'user_1': [0.5, 0.3], 'user_2': [0.9, 0.1]}
        self.training_complete = True
        return self

# Save trained model
model = RecommendationModel()
model.train(large_dataset)
with open('trained_model.pkl', 'wb') as f:
    pickle.dump(model, f)

# Later, in a different script
with open('trained_model.pkl', 'rb') as f:
    loaded_model = pickle.load(f)
print(loaded_model.training_complete)  # True

The Gotchas You Need to Know

pickle isn't perfect, and pretending otherwise would be dishonest. Here are the main things to watch out for:

Security First

Never, ever unpickle data from untrusted sources. Pickled data can execute arbitrary code when deserialized. It's not encrypted, signed, or validated in any way. If someone sends you a .pkl file from an email attachment, treat it like a suspicious executable.

Version Compatibility

Pickles created with Python 2 may not load in Python 3, and vice versa. Even between Python 3.x versions, you might encounter issues. Always test your pickled files in the environment where they'll be used.

Class Definitions Must Exist

When you unpickle a class instance, Python needs the class definition to be available. If you've deleted or renamed the class, your pickle file becomes useless. This is a common headache for developers who refactor their codebase.

Not Human-Readable

Unlike JSON or YAML, pickle files are binary gibberish. You can't open them in a text editor and understand what's inside. For configuration files or data exchange between different programming languages, stick with JSON.

When Should You Actually Use pickle?

Here's my honest take from years of Python development:

Use pickle for: - Caching intermediate results in long-running computations - Saving trained machine learning models - Persisting application state between sessions - Storing complex Python objects where performance matters

Avoid pickle for: - Data exchange between different systems or languages - Configuration files that humans need to edit - Long-term archival (formats change, and you might lose access) - Any scenario involving untrusted data sources

A Practical Alternative

For most data persistence needs, the PythonSkillset team actually recommends starting with JSON, then moving to pickle only when you need it. Here's why: JSON is human-readable, language-agnostic, and far safer. You lose the ability to serialize arbitrary objects, but for 90% of use cases, that's fine.

import json

data = {'name': 'skillset', 'version': 2024}
with open('data.json', 'w') as f:
    json.dump(data, f)

# Human can read it!
# {"name": "skillset", "version": 2024}

The Bottom Line

pickle is a powerful tool that every Python developer should understand. It's not the right choice for every situation, but when you need to save and restore complex Python object graphs with minimal fuss, nothing beats it. Just remember the security implications and version concerns, and you'll be fine.

Next time you're building a Python application that needs to remember its state between runs, give pickle a try. It might just save you hours of rework.

Comments

Questions, corrections, and tips stay visible for everyone reading this page.

0 in thread

Join the discussion

Shown next to your comment.

Up to 4,000 characters

No comments yet

Be the first to leave a note — it helps the next reader.