| 1 |
# cloudpickle |
| 2 |
|
| 3 |
[](https://github.com/cloudpipe/cloudpickle/actions) |
| 4 |
[](https://codecov.io/github/cloudpipe/cloudpickle?branch=master) |
| 5 |
|
| 6 |
`cloudpickle` makes it possible to serialize Python constructs not supported |
| 7 |
by the default `pickle` module from the Python standard library. |
| 8 |
|
| 9 |
`cloudpickle` is especially useful for **cluster computing** where Python |
| 10 |
code is shipped over the network to execute on remote hosts, possibly close |
| 11 |
to the data. |
| 12 |
|
| 13 |
Among other things, `cloudpickle` supports pickling for **lambda functions** |
| 14 |
along with **functions and classes defined interactively** in the |
| 15 |
`__main__` module (for instance in a script, a shell or a Jupyter notebook). |
| 16 |
|
| 17 |
Cloudpickle can only be used to send objects between the **exact same version |
| 18 |
of Python**. |
| 19 |
|
| 20 |
Using `cloudpickle` for **long-term object storage is not supported and |
| 21 |
strongly discouraged.** |
| 22 |
|
| 23 |
**Security notice**: one should **only load pickle data from trusted sources** as |
| 24 |
otherwise `pickle.load` can lead to arbitrary code execution resulting in a critical |
| 25 |
security vulnerability. |
| 26 |
|
| 27 |
|
| 28 |
Installation |
| 29 |
------------ |
| 30 |
|
| 31 |
The latest release of `cloudpickle` is available from |
| 32 |
[pypi](https://pypi.python.org/pypi/cloudpickle): |
| 33 |
|
| 34 |
pip install cloudpickle |
| 35 |
|
| 36 |
|
| 37 |
Examples |
| 38 |
-------- |
| 39 |
|
| 40 |
Pickling a lambda expression: |
| 41 |
|
| 42 |
```python |
| 43 |
>>> import cloudpickle |
| 44 |
>>> squared = lambda x: x ** 2 |
| 45 |
>>> pickled_lambda = cloudpickle.dumps(squared) |
| 46 |
|
| 47 |
>>> import pickle |
| 48 |
>>> new_squared = pickle.loads(pickled_lambda) |
| 49 |
>>> new_squared(2) |
| 50 |
4 |
| 51 |
``` |
| 52 |
|
| 53 |
Pickling a function interactively defined in a Python shell session |
| 54 |
(in the `__main__` module): |
| 55 |
|
| 56 |
```python |
| 57 |
>>> CONSTANT = 42 |
| 58 |
>>> def my_function(data: int) -> int: |
| 59 |
... return data + CONSTANT |
| 60 |
... |
| 61 |
>>> pickled_function = cloudpickle.dumps(my_function) |
| 62 |
>>> depickled_function = pickle.loads(pickled_function) |
| 63 |
>>> depickled_function |
| 64 |
<function __main__.my_function(data:int) -> int> |
| 65 |
>>> depickled_function(43) |
| 66 |
85 |
| 67 |
``` |
| 68 |
|
| 69 |
|
| 70 |
Overriding pickle's serialization mechanism for importable constructs: |
| 71 |
---------------------------------------------------------------------- |
| 72 |
|
| 73 |
An important difference between `cloudpickle` and `pickle` is that |
| 74 |
`cloudpickle` can serialize a function or class **by value**, whereas `pickle` |
| 75 |
can only serialize it **by reference**. Serialization by reference treats |
| 76 |
functions and classes as attributes of modules, and pickles them through |
| 77 |
instructions that trigger the import of their module at load time. |
| 78 |
Serialization by reference is thus limited in that it assumes that the module |
| 79 |
containing the function or class is available/importable in the unpickling |
| 80 |
environment. This assumption breaks when pickling constructs defined in an |
| 81 |
interactive session, a case that is automatically detected by `cloudpickle`, |
| 82 |
that pickles such constructs **by value**. |
| 83 |
|
| 84 |
Another case where the importability assumption is expected to break is when |
| 85 |
developing a module in a distributed execution environment: the worker |
| 86 |
processes may not have access to the said module, for example if they live on a |
| 87 |
different machine than the process in which the module is being developed. By |
| 88 |
itself, `cloudpickle` cannot detect such "locally importable" modules and |
| 89 |
switch to serialization by value; instead, it relies on its default mode, which |
| 90 |
is serialization by reference. However, since `cloudpickle 2.0.0`, one can |
| 91 |
explicitly specify modules for which serialization by value should be used, |
| 92 |
using the |
| 93 |
`register_pickle_by_value(module)`/`/unregister_pickle_by_value(module)` API: |
| 94 |
|
| 95 |
```python |
| 96 |
>>> import cloudpickle |
| 97 |
>>> import my_module |
| 98 |
>>> cloudpickle.register_pickle_by_value(my_module) |
| 99 |
>>> cloudpickle.dumps(my_module.my_function) # my_function is pickled by value |
| 100 |
>>> cloudpickle.unregister_pickle_by_value(my_module) |
| 101 |
>>> cloudpickle.dumps(my_module.my_function) # my_function is pickled by reference |
| 102 |
``` |
| 103 |
|
| 104 |
Using this API, there is no need to re-install the new version of the module on |
| 105 |
all the worker nodes nor to restart the workers: restarting the client Python |
| 106 |
process with the new source code is enough. |
| 107 |
|
| 108 |
Note that this feature is still **experimental**, and may fail in the following |
| 109 |
situations: |
| 110 |
|
| 111 |
- If the body of a function/class pickled by value contains an `import` statement: |
| 112 |
```python |
| 113 |
>>> def f(): |
| 114 |
>>> ... from another_module import g |
| 115 |
>>> ... # calling f in the unpickling environment may fail if another_module |
| 116 |
>>> ... # is unavailable |
| 117 |
>>> ... return g() + 1 |
| 118 |
``` |
| 119 |
|
| 120 |
- If a function pickled by reference uses a function pickled by value during its execution. |
| 121 |
|
| 122 |
|
| 123 |
Running the tests |
| 124 |
----------------- |
| 125 |
|
| 126 |
- With `tox`, to test run the tests for all the supported versions of |
| 127 |
Python and PyPy: |
| 128 |
|
| 129 |
pip install tox |
| 130 |
tox |
| 131 |
|
| 132 |
or alternatively for a specific environment: |
| 133 |
|
| 134 |
tox -e py312 |
| 135 |
|
| 136 |
|
| 137 |
- With `pytest` to only run the tests for your current version of |
| 138 |
Python: |
| 139 |
|
| 140 |
pip install -r dev-requirements.txt |
| 141 |
PYTHONPATH='.:tests' pytest |
| 142 |
|
| 143 |
History |
| 144 |
------- |
| 145 |
|
| 146 |
`cloudpickle` was initially developed by [picloud.com](http://web.archive.org/web/20140721022102/http://blog.picloud.com/2013/11/17/picloud-has-joined-dropbox/) and shipped as part of |
| 147 |
the client SDK. |
| 148 |
|
| 149 |
A copy of `cloudpickle.py` was included as part of PySpark, the Python |
| 150 |
interface to [Apache Spark](https://spark.apache.org/). Davies Liu, Josh |
| 151 |
Rosen, Thom Neale and other Apache Spark developers improved it significantly, |
| 152 |
most notably to add support for PyPy and Python 3. |
| 153 |
|
| 154 |
The aim of the `cloudpickle` project is to make that work available to a wider |
| 155 |
audience outside of the Spark ecosystem and to make it easier to improve it |
| 156 |
further notably with the help of a dedicated non-regression test suite. |