The lz4 library provides Python bindings for the high-performance LZ4 compression algorithm by Yann Collet. It supports both the frame and block formats, with the frame format being recommended for most applications due to its interoperability. The library is actively maintained with frequent releases, currently at version 4.4.5, and offers a Pythonic API that can serve as a drop-in alternative to standard library compression modules like `zlib` or `gzip`.
pip install lz4Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic compression and decompression using the recommended `lz4.frame` module. It generates random bytes, compresses them into an LZ4 frame, then decompresses the data and verifies its integrity. Note that input data must be in bytes format.
Ensure data is of type `bytes` before passing to compression functions.
For `lz4.block.decompress`, either provide the exact `uncompressed_size` or implement a loop with `max_size` and error handling for `LZ4BlockError`.
Prioritize `import lz4.frame` and its `compress`/`decompress` functions for broad compatibility unless specific block-level control is required.
Use `lz4.frame` for stream-like or file-based compression, which offers robust and maintained functionality for large data handling.
Benchmark and explicitly set `compression_level` and `block_size` based on your application's specific performance and compression ratio requirements.
Ensure a C compiler is available in your build environment. For Alpine-based Python images (like `python:3.13-alpine`), this usually means installing the `build-base` package (e.g., `apk add build-base`) before running `pip install lz4`.
Ensure the lz4 library is correctly installed in your environment: `pip install lz4`
Verify the integrity of the compressed data. If using `lz4.block.decompress`, ensure the `uncompressed_size` argument is accurate, or use `lz4.frame.decompress` if the data was compressed using the frame format, which usually embeds size information. Example of handling unknown size: `while True: try: decompressed = lz4.block.decompress(compressed, uncompressed_size=usize) break except lz4.block.LZ4BlockError: usize *= 2`
Encode the Python string to bytes before compressing and decode the resulting bytes back to a string after decompressing. Example: `original_string.encode('utf-8')` before compression, and `decompressed_bytes.decode('utf-8')` after decompression.Install the missing 'deprecation' package: `pip install deprecation`. Alternatively, upgrade 'lz4' to a newer version which might no longer have this dependency or handles it more robustly: `pip install --upgrade lz4`.