{"id":57809,"date":"2023-12-28T08:29:18","date_gmt":"2023-12-28T08:29:18","guid":{"rendered":"https:\/\/www.askpython.com\/?p=57809"},"modified":"2025-04-10T20:49:06","modified_gmt":"2025-04-10T20:49:06","slug":"install-pickle-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/install-pickle-python","title":{"rendered":"How to Install and Use Pickle for Python 3"},"content":{"rendered":"\n<p>Pickle is an incredibly useful Python module for serializing and deserializing Python object structures. By \u201cserializing\u201d, we mean converting a Python object hierarchy into a byte stream. And by \u201cdeserializing\u201d, we mean reconstructing the object hierarchy from the byte stream. In this guide, we will cover these topics in detail using practical, real-world examples you can try for yourself. By the end, you\u2019ll be a Pickle pro!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-pickle-and-why-use-it\">What is Pickle, and Why Use It?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/pickle-module-python\" data-type=\"post\" data-id=\"1280\">Pickle<\/a> allows you to save Python objects to files (or other storage) and load them later. This is incredibly handy for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Persisting Python objects across program runs<\/strong>: Store an object now, close the program, and then access the object later by loading it. This lets you resume right where you left off!<\/li>\n\n\n\n<li><strong>Sending Python objects across networks<\/strong>: Serialize objects on one computer, send the bytes over the network, and then deserialize them on another computer.<\/li>\n\n\n\n<li><strong>Storing Python objects in databases<\/strong>: Serialize and store objects in database blob columns. Works great with NoSQL databases.<\/li>\n<\/ul>\n\n\n\n<p>Without Pickle, you must manually extract object data into simple primitive types before saving or sending. And manually reassembled into complex objects afterward. This gets tedious fast!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"installing-pickle\">Installing Pickle on Python<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>The good news is that&nbsp;<strong>Pickle comes pre-installed with Python 3<\/strong>! There\u2019s nothing extra to install.<\/em><\/p>\n<\/blockquote>\n\n\n\n<p>To confirm this, open up a Python 3 interpreter:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pickle\nprint(pickle.__version__)\n\n<\/pre><\/div>\n\n\n<p>This prints out your installed Pickle version. In Python 3, this will likely be something like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n1.0\n\n<\/pre><\/div>\n\n\n<p>And that\u2019s it! Pickle is ready to use.<\/p>\n\n\n\n<p>The only thing you may need to install is&nbsp;<code>PickleDB<\/code>&nbsp;if you want to easily serialize Python objects to a simple key-value style database powered by Pickle.<\/p>\n\n\n\n<p>You can install PickleDB via pip:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install pickledb\n\n<\/pre><\/div>\n\n\n<p>Otherwise, vanilla Pickle can go out of the box with Python 3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"serializing-python-objects-with-pickle\">Serializing Python Objects with Pickle<\/h2>\n\n\n\n<p>Serializing objects is where Pickle really shines.<\/p>\n\n\n\n<p>The main function for this is&nbsp;<code>pickle.dumps()<\/code>. It serializes a Python object hierarchy into a stream of bytes.<\/p>\n\n\n\n<p>Here\u2019s a quick example of serializing a simple <a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\" data-type=\"post\" data-id=\"535\">Python dict<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pickle\n\ndata = {\n  &#039;name&#039;: &#039;Daniel&#039;,\n  &#039;age&#039;: 25,\n  &#039;favorite_colors&#039;: &#x5B;&#039;blue&#039;, &#039;green&#039;]\n}\n\nserialized_data = pickle.dumps(data)\n\nprint(serialized_data)\n\n<\/pre><\/div>\n\n\n<p>Which outputs bytes:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nb&#039;\\x80\\x04\\x95\\x1c\\x00\\x00\\x00\\x00\\x00\\x00\\x00}\\x94(X\\x04\\x00\\x00\\x00nameq\\x01X\\x06\\x00\\x00\\x00Danielq\\x02X\\x03\\x00\\x00\\x00ageq\\x03K\\x19X\\x11\\x00\\x00\\x00favorite_colorsq\\x04]q\\x05(X\\x04\\x00\\x00\\x00blueq\\x06X\\x05\\x00\\x00\\x00greenq\\x07eu.&#039;\n\n<\/pre><\/div>\n\n\n<p>We could then write these bytes to a file, database, network socket etc. When we want to access the data again, we just deserialize!<\/p>\n\n\n\n<p>Some key notes on&nbsp;<code>pickle.dumps()<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Accepts a Python object hierarchy<\/li>\n\n\n\n<li>Returns a&nbsp;<strong>bytes<\/strong>&nbsp;object<\/li>\n\n\n\n<li>Use&nbsp;<code>pickle.loads()<\/code>&nbsp;to deserialize the bytes back into objects<\/li>\n<\/ul>\n\n\n\n<p>You can serialize&nbsp;<strong>nearly any<\/strong>&nbsp;Python object like this. For example NumPy arrays, Pandas DataFrames, custom classes, and more. Pickle just recurses through the object structure and serializes it out.<\/p>\n\n\n\n<p>There are also complementary methods like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pickle.dump()<\/code>&nbsp;&#8211; serialize object to open file<\/li>\n\n\n\n<li><code>pickle.load()<\/code>&nbsp;&#8211; deserialize from open file back into object<\/li>\n<\/ul>\n\n\n\n<p>But&nbsp;<code>pickle.dumps()\/loads()<\/code>&nbsp;give you the bytes to work with manually.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/loading-pickled-pandas-object\" data-type=\"post\" data-id=\"40603\">How to Load Pickled Pandas Object From a File as a Path?<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"deserializing-byte-streams-into-python-objects\">Deserializing Byte Streams into Python Objects<\/h2>\n\n\n\n<p>Once you have serialized bytes, you can reconstitute them back into live Python objects using&nbsp;<code>pickle.loads()<\/code>.<\/p>\n\n\n\n<p>Continuing the example above:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pickle\n\n# Serialized bytes from previous example\nserialized_data = b&#039;\\x80\\x04\\x95\\x1c\\x00\\x00\\x00\\x00\\x00\\x00\\x00}\\x94(X\\x04\\x00\\x00\\x00nameq\\x01X\\x06\\x00\\x00\\x00Danielq\\x02X\\x03\\x00\\x00\\x00ageq\\x03K\\x19X\\x11\\x00\\x00\\x00favorite_colorsq\\x04]q\\x05(X\\x04\\x00\\x00\\x00blueq\\x06X\\x05\\x00\\x00\\x00greenq\\x07eu.&#039;\n\n# Deserialize bytes back into a Python dict\ndeserialized_data = pickle.loads(serialized_data)\n\nprint(deserialized_data)\nprint(type(deserialized_data))\n\n<\/pre><\/div>\n\n\n<p>Prints out the reconstructed data:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{&#039;name&#039;: &#039;Daniel&#039;, &#039;age&#039;: 25, &#039;favorite_colors&#039;: &#x5B;&#039;blue&#039;, &#039;green&#039;]}\n&lt;class &#039;dict&#039;&gt;\n\n<\/pre><\/div>\n\n\n<p>We get back the&nbsp;<strong>exact same dict<\/strong>, thanks to Pickle!<\/p>\n\n\n\n<p>The complementary method to&nbsp;<code>pickle.loads()<\/code>&nbsp;is&nbsp;<code>pickle.load()<\/code>&nbsp;which deserializes from an open file.<\/p>\n\n\n\n<p>So in summary, core serialization functions are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pickle.dumps()<\/code>&nbsp;&#8211; serialize to bytes<\/li>\n\n\n\n<li><code>pickle.loads()<\/code>&nbsp;&#8211; deserialize from bytes<\/li>\n\n\n\n<li><code>pickle.dump()<\/code>&nbsp;&#8211; serialize to file<\/li>\n\n\n\n<li><code>pickle.load()<\/code>&nbsp;&#8211; deserialize from file<\/li>\n<\/ul>\n\n\n\n<p>Use these to get Python objects in and out of byte streams.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/pandas-to-pickle\" data-type=\"post\" data-id=\"40835\">Pandas to_pickle(): Pickle (serialize) object to File<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"pickle-best-practices-and-downsides\">Pickle Best Practices and Downsides<\/h2>\n\n\n\n<p>Pickle is enormously useful, but does come with some downsides to be aware of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Security vulnerability<\/strong>: Pickle allows arbitrary code execution during deserialization by reconstructing Python bytecode. Only unpickle data you trust!<\/li>\n\n\n\n<li><strong>Python version dependence<\/strong>: Pickle byte streams are specific to a Python version. So, Python 2 pickles may not load in Python 3.<\/li>\n\n\n\n<li><strong>Class definition dependence<\/strong>: The original class definitions must be importable during deserialization. So, if you refactor code, Pickle may break trying to load old data.<\/li>\n<\/ul>\n\n\n\n<p>The big one is security. Deserializing untrusted data can execute malicious attacks by reconstructing Python bytecode inside your process.<\/p>\n\n\n\n<p>So&nbsp;<strong>only deserialize Pickle streams from trusted sources<\/strong>! For example, data you serialized yourself or from secured databases.<\/p>\n\n\n\n<p>Avoid loading random Pickle streams from unknown Internet sources. Consider using JSON for untrusted data as it does not support arbitrary code execution during deserialization.<\/p>\n\n\n\n<p>Beyond this, pickle streams will likely only work on the same Python version and require the original class definitions to reconstruct objects. So you need to be careful when upgrading Python versions or refactoring code.<\/p>\n\n\n\n<p><strong>But used correctly on trusted data &#8211; Pickle is incredibly handy!<\/strong><\/p>\n\n\n\n<p>It can serialize nearly any Python object while allowing convenient data storage, network transmission, and workflow persistence.<\/p>\n\n\n\n<p>I hope you now feel empowered to start using Pickle for your Python workflows! Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pickle is an incredibly useful Python module for serializing and deserializing Python object structures. By \u201cserializing\u201d, we mean converting a Python object hierarchy into a byte stream. And by \u201cdeserializing\u201d, we mean reconstructing the object hierarchy from the byte stream. In this guide, we will cover these topics in detail using practical, real-world examples you [&hellip;]<\/p>\n","protected":false},"author":77,"featured_media":64116,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-57809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=57809"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/57809\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/64116"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=57809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=57809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=57809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}