compress-pickle is a Python library (version 2.1.0) that thinly wraps the standard `pickle` package with various standard compression libraries (gzip, bz2, lzma, zipfile, and optionally lz4). It provides an interface similar to `pickle.dump`, `pickle.load`, `pickle.dumps`, and `pickle.loads` to seamlessly serialize and deserialize Python objects to disk or file-like objects in a compressed manner. The library has an infrequent release cadence, with its last major update in September 2021.
pip install compress-pickleVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `compress_pickle.dump` to serialize and compress a Python dictionary to a file, and `compress_pickle.load` to decompress and deserialize it back. The compression method (gzip in this case) is automatically inferred from the file extension '.pkl.gz'.
Upgrade Python to 3.6+ or pin `compress-pickle` to version `1.1.1` for Python 3.5 environments.
Only unpickle data from trusted sources. Consider alternative serialization formats like JSON for untrusted data.
Evaluate file sizes for your specific use case. Compression is generally beneficial for larger objects.
Always use appropriate file extensions or explicitly pass the `compression` argument to `dump` and `load`.
Be mindful of the Python versions used for pickling and unpickling. Specify a compatible `pickler_kwargs={'protocol': N}` if cross-version compatibility is critical.Run `pip install compress-pickle` in your terminal to install the library.
Ensure you are loading the file with the same compression protocol used during saving. If the compression was inferred from the file extension, ensure the extension is correct. Example: `data = compress_pickle.load('file.pkl.gz', compression='gzip')`Verify that the compression argument in `compress_pickle.load()` matches the compression method used when the file was saved. Also, check the integrity of the compressed file. Example: `data = compress_pickle.load('my_data.pkl.bz2', compression='bz2')`Try using `compression='gzip'` for large NumPy arrays or explicitly specify an older pickle protocol (e.g., `protocol=4`) in `compress_pickle.dump` and `compress_pickle.load` to avoid issues with `PickleBuffer` handling. Example: `compress_pickle.dump(data, 'file.pkl.lz4', compression='lz4', protocol=4)`