Backports Zstandard (zstd)

1.3.0 · active · verified Sun Mar 29

backports-zstd is a library that backports the `compression.zstd` module, introduced in Python 3.14, to older Python versions (3.9 to 3.13). It provides a Pythonic interface for Zstandard compression, aiming to be as close as possible to the standard library's implementation. The library is currently at version 1.3.0 and is actively maintained, though it is designed to be superseded by the standard library module in Python 3.14 and later.

Warnings

Install

Imports

Quickstart

Demonstrates how to conditionally import the `zstd` module and perform basic compression and decompression of byte data. This pattern ensures your code works across Python versions where `compression.zstd` is either in the standard library or provided by the backport.

import sys

if sys.version_info >= (3, 14):
    from compression import zstd
else:
    from backports import zstd

original_data = b"Hello, Zstandard backport! " * 100

# Compress data
compressed_data = zstd.compress(original_data)
print(f"Original size: {len(original_data)} bytes")
print(f"Compressed size: {len(compressed_data)} bytes")

# Decompress data
decompressed_data = zstd.decompress(compressed_data)
print(f"Decompressed size: {len(decompressed_data)} bytes")
assert original_data == decompressed_data

view raw JSON →