Backports Zstandard (zstd)
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
- breaking This library is intentionally not installable on Python 3.14 or later. For Python 3.14+, the `compression.zstd` module is part of the standard library and should be used directly.
- deprecated The `backports.zstd` library is scheduled for deprecation with the end of official support for Python 3.13 (October 2029).
- gotcha For code compatible with both Python versions below 3.14 and 3.14+, a conditional import statement is required to correctly import the `zstd` module.
- gotcha By default, `backports.zstd` uses a statically bundled `libzstd`. If you prefer to use a system-installed `libzstd`, you must pass a specific argument during the build phase.
Install
-
pip install "backports.zstd; python_version < '3.14'" -
pip install backports.zstd
Imports
- zstd
import sys if sys.version_info >= (3, 14): from compression import zstd else: from backports import zstd - tarfile
import sys if sys.version_info >= (3, 14): import tarfile else: from backports.zstd import tarfile - zipfile
import sys if sys.version_info >= (3, 14): import zipfile else: from backports.zstd import zipfile - register_shutil
import sys if sys.version_info < (3, 14): from backports.zstd import register_shutil register_shutil()
Quickstart
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