Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Core Tech
Labs

Guided: Python Standard Library

In this Guided Code Lab, you will enhance a recipe book application to persist data between sessions using the pickle module in Python. This process, known as data serialization, is crucial for applications that need to save and load data. You'll learn about serialization, explore the pickle module, handle errors during pickling, and use the Pickler and Unpickler classes for better control over the serialization process.

Lab platform
Lab Info
Level
Beginner
Last updated
Dec 23, 2025
Duration
30m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use, and consent to receive marketing emails from Pluralsight.
Table of Contents
  1. Challenge

    Introduction

    Introduction

    Welcome to the Guided: Python Standard Library - Data Serialization lab 🚀.

    In this Guided Code Lab, you will enhance a recipe book application to persist data between sessions using the pickle module in Python. This process, known as data serialization, is crucial for applications that need to save and load data. You'll learn about serialization, explore the pickle module, handle errors during pickling, and use the Pickler and Unpickler classes for better control over the serialization process.

    Pre-requisites

    • Basic understanding of Python programming.

    Learning Objectives

    • Understand data serialization and its importance.
    • Explore the Pickle module in Python for data serialization.
    • Implement and handle errors during pickling and unpickling.
    • Enhance a recipe book application for data persistence between sessions.

    Running The App

    • You can run the Cookbook app in the terminal with the command python3 cookbook.py
  2. Challenge

    Data Serialization Concepts

    Introduction to Data Serialization

    Understanding Data Serialization is fundamental as it facilitates the conversion of in-memory objects into a format that can be saved to disk or sent over a network, and later reconstructed. This is crucial for developing applications that require data persistence or inter-process communication.

    Hands On - JSON vs Pickle

    Let's explore two common ways to serialize data with Python, JSON and Pickle

    In the terminal to your right, execute the following lines of code.

    import json
    import pickle
    
    # Serialization using JSON
    data = {"name": "John", "age": 30}
    json_serialized = json.dumps(data)
    print(json_serialized)  # Outputs: {"name": "John", "age": 30}
    
    # Deserialization using JSON
    json_deserialized = json.loads(json_serialized)
    print(json_deserialized)  # Outputs: {'name': 'John', 'age': 30}
    
    # Serialization using Pickle
    pickle_serialized = pickle.dumps(data)
    print(pickle_serialized)  # Outputs: b'\x80\x04\x95\x1f\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x04name\x94\x8c\x04John\x94\x8c\x03age\x94K\x1eu.'
    
    # Deserialization using Pickle
    pickle_deserialized = pickle.loads(pickle_serialized)
    print(pickle_deserialized)  # Outputs: {'name': 'John', 'age': 30}
    

    Summary

    JSON: Human-readable and textual format. Ideal for configuration files, data exchange between client-server, and different language environments. However, it doesn't support binary data and may not preserve data types for complex objects.

    Pickle: Binary format, capable of serializing complex Python objects, and supports binary data. Ideal for serializing Python objects, but it's Python-specific and not human-readable.

    Data Type Preservation Example

    Let's dig a little deeper and take a look at an example showing data type preservation in Pickle vs JSON.

    complex_data = (1 + 2j)  # Complex number
    pickle_serialized = pickle.dumps(complex_data)
    # This will raise a TypeError as JSON can't serialize complex numbers
    json_serialized = json.dumps(complex_data)
    pickle_deserialized = pickle.loads(pickle_serialized)  # This will preserve the complex number data type
    

    Security Warning

    The pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.

    The choice between JSON and Pickle, or other serialization formats, depends on the use case, with considerations for human-readability, data complexity, language interoperability, and security. In this lab we are going to focus on using Pickle. Let's get started!

  3. Challenge

    Setup and Import

    Implementing Load and Save Functions

    Having the ability to load and save data is crucial for providing a persistent state in applications. This section explores creating dedicated functions to handle loading and saving of data in your recipe book application.

    All code will be written in cookbook.py

    Part 1: Importing Necessary Modules

    Task

    1. Import the pickle module which will provide the necessary classes and functions for this lab.
    import pickle
    
  4. Challenge

    Loading Recipes with Unpickler

    Part 2: Loading Recipes with Unpickler

    The Unpickler class in the pickle module is used to deserialize objects from a binary stream. In this part, you'll create a function to load recipes from a file using the Unpickler class.

    Tasks

    1. Define a function named load_recipes with a parameter filename.
    2. Within the function, use a try block to attempt to open the file specified by filename in binary read mode ('rb').
    3. Create an instance of pickle.Unpickler, passing the file object to it.
    4. Use the load method of the Unpickler instance to deserialize the data from the file.
    5. Return the deserialized data.
    6. Use an except block to catch a FileNotFoundError exception, returning an empty list in case the file doesn't exist.

    đź’ˇ Hint: Peek at the solution code in solution/cookbook.py if you are stuck.

  5. Challenge

    Saving Recipes with Pickler

    Part 3: Saving Recipes with Pickler

    The Pickler class in the pickle module is used to serialize objects into a binary stream. In this part, you'll create a function to save recipes to a file using the Pickler class.

    Tasks

    1. Define a function named save_recipes with parameters recipes and filename.
    2. Within the function, use a try block to attempt to open the file specified by filename in binary write mode ('wb').
    3. Create an instance of pickle.Pickler, passing the file object to it.
    4. Use the dump method of the Pickler instance to serialize the recipes data into the file.
    5. Use an except block to catch a pickle.PicklingError exception, printing an error message in case the serialization fails.

    đź’ˇ Hint: Peek at the solution code in solution/cookbook.py if you are stuck.

  6. Challenge

    Update Main Function

    Part 4: Update the Main Function

    Tasks

    1. Call load_recipes at the start of your main function and save_recipes before exiting the application.

    This will ensure that your recipe data persists between application sessions, providing a much better user experience.

    Pickling without a file can be useful in situations where you need to send serialized data over a network or save it to a database. Python's pickle module provides dumps() and loads() methods for this purpose.

    In the terminal to the right, run the following commands.

    Hands On

    # Pickling to a bytes object
    pickled_data = pickle.dumps(data)
    
    # Unpickling from a bytes object
    unpickled_data = pickle.loads(pickled_data)
    

    Potential Pitfalls

    It's important to ensure that the serialized data does not get corrupted or altered as it could lead to errors during deserialization.

    # Example of a PicklingError
    try:
        bad_data = pickle.loads(b"not a pickled object")
    except pickle.PicklingError as e:
        print(f"PicklingError: {e}")
    

    In summary, pickling without a file is a flexible way to work with serialized data in different contexts beyond file operations.

  7. Challenge

    Bytes Serialization Implementation

    Your Turn: Implementing Bytes Serialization

    In this part, you'll add the ability to pickle and unpickle data to and from bytes objects in your application.

    Part 5: Serializing Data to Bytes Objects

    The pickle.dumps() function is used to serialize an object to a bytes object, instead of writing it to a file.

    Task

    1. Define a function named pickle_to_bytes with a parameter recipes.
    2. Within the function, use pickle.dumps() to serialize the recipes data to a bytes object.
    3. Return the serialized bytes object.

    Part 6: Deserializing Data from Bytes Objects

    The pickle.loads() function is used to deserialize a bytes object back to its original object form.

    Task

    1. Define a function named unpickle_from_bytes with a parameter pickled_data.
    2. Within the function, use pickle.loads() to deserialize the pickled_data bytes object back to its original form.
    3. Return the deserialized object.

    Best Practices

    Be cautious when using the global keyword, as it can lead to code that is difficult to debug or maintain. It's often better to pass variables as arguments to functions where possible.

    # It's often better to pass variables as arguments
    def save_data(data, filename):
        with open(filename, 'wb') as file:
            pickle.dump(data, file)
    
    # Calling the function with arguments
    save_data(recipes, filename)
    
  8. Challenge

    Summary and Next Steps

    Summary

    Congratulations on completing the lab! 🎉 In this lab, you've learned about:

    1. Data Serialization: The process of converting in-memory objects into a format that can be saved to disk or transmitted over a network, and later reconstructed.
    2. Pickle Module: A Python module that provides functionalities for serializing and deserializing Python objects.
    3. Implementing Load and Save Functions: Created functions to handle the loading and saving of data to ensure the persistence of application state.
    4. Pickling Without a File: How to serialize and deserialize data to and from bytes objects, which is useful in scenarios such as network transmission or database storage.
    5. Modifying the Main Function: Incorporated loading and saving recipes in the main function to ensure data persistence across different runs of the application.

    Throughout the lab, you not only explored the theoretical concepts but also applied them practically in a Recipe Book application. This hands-on approach helped in solidifying the understanding of data serialization using Python's Pickle module, and how it's instrumental in building applications with data persistence.

    Next Steps

    To further your understanding and skills, you might want to:

    • Explore other serialization libraries in Python such as json and marshal.
    • Look into security best practices when working with serialization, especially when handling data from untrusted sources.
    • Experiment with more complex data structures and see how they are handled by the pickle module.
    • Explore how to work with files in Python, including reading from and writing to files, which is a crucial part of data serialization.
About the author

Danny Sullivan is a former special education teacher and professional baseball player that moved into software development in 2014. He’s experienced with Ruby, Python and JavaScript ecosystems, but enjoys Ruby most for its user friendliness and rapid prototyping capabilities.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight