Registry / serialization / brotlipy

brotlipy

JSON →
library0.7.0pypypi✓ verified 24d ago

brotlipy is a Python library that provides CFFI-based bindings to Google's Brotli lossless compression algorithm. It enables Python applications to efficiently compress and decompress data using Brotli, supporting both one-shot and streaming operations. Version 0.7.0 is the final release of this library, as the project has been archived and its development continues under the `brotlicffi` package.

pip install brotlipy
INSTALL
IMPORT
SIG · BROTLIPY
B
brotlipy
serializationpythonv0.7.0
Install
1.9s avg
Import
Disk
21MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.000s · 23MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

brotli
import brotli
import brotlipy
The package installs as `brotlipy` but exposes its functionality under the `brotli` module name, which can lead to conflicts with other `brotli` packages.
compress
brotli.compress(data)
For one-shot compression.
decompress
brotli.decompress(data)
For one-shot decompression.
Compressor
brotli.Compressor()
For streaming compression.
Decompressor
brotli.Decompressor()
For streaming decompression.
Error
brotli.Error
Exception raised for compression/decompression errors.

This quickstart demonstrates both one-shot and streaming compression/decompression using `brotlipy`. For one-shot operations, `brotli.compress()` and `brotli.decompress()` are used. For streaming, `brotli.Compressor()` and `brotli.Decompressor()` objects are initialized, and data is processed in chunks. Remember that `brotli.compress()` and `brotli.decompress()` expect and return byte strings.

import brotli original_data = b"This is some data to be compressed using Brotli. It can be any bytes string." # One-shot compression and decompression compressed_data = brotli.compress(original_data, quality=11) # quality from 0-11 decompressed_data = brotli.decompress(compressed_data) print(f"Original size: {len(original_data)} bytes") print(f"Compressed size: {len(compressed_data)} bytes") print(f"Decompressed data matches original: {original_data == decompressed_data}") # Streaming compression compressor = brotli.Compressor(quality=5) compressed_stream = b'' chunk_size = len(original_data) // 2 compressed_stream += compressor.compress(original_data[:chunk_size]) compressed_stream += compressor.compress(original_data[chunk_size:]) compressed_stream += compressor.finish() # Streaming decompression decompressor = brotli.Decompressor() decompressed_stream = b'' decompressed_stream += decompressor.decompress(compressed_stream[:chunk_size]) decompressed_stream += decompressor.decompress(compressed_stream[chunk_size:]) decompressed_stream += decompressor.flush() # flush() is deprecated for Decompressor since 0.4.0 but shown in older docs print(f"Streaming decompression matches original: {original_data == decompressed_stream}")
Debug
Known issues
breakingThe `brotlipy` package has been archived and is deprecated. All further development and updates have moved to the `brotlicffi` package. Users are strongly advised to migrate to `brotlicffi` for continued support and new features.
fix
Migrate to `brotlicffi`: `pip install brotlicffi`. Update imports from `import brotli` to `import brotlicffi`. Refer to `brotlicffi` documentation for any API changes.
affects: 0.7.0 and earlier
gotchaThe `brotlipy` library installs as `brotlipy` but its main module is imported as `brotli`. This can create import conflicts if another Python package also provides a top-level `brotli` module (e.g., the pure-Python `brotli` package). The successor `brotlicffi` addresses this by using `import brotlicffi`.
fix
Be aware of potential module name collisions. When migrating to `brotlicffi`, the import path changes to `import brotlicffi`.
affects: All versions of brotlipy
gotchaOn Linux systems, `brotlipy` is a CFFI-based binding that requires compilation during installation. This necessitates having a C compiler (e.g., `build-essential` or `gcc`), Python development headers (`python-dev` or `python-devel`), and the `libffi` development library (`libffi-dev` or `libffi-devel`) installed on the system.
fix
Install the necessary system-level development packages before running `pip install brotlipy`. Examples: `sudo apt-get install build-essential python-dev libffi-dev` (Debian/Ubuntu) or `sudo yum install gcc libffi-devel python-devel` (Red Hat/CentOS).
affects: All versions of brotlipy
gotchaWhen using dictionaries with the compression and decompression APIs, it is crucial that the *exact same dictionary* is provided to both the compressor and the decompressor. A mismatch will lead to decompression errors or corrupted output.
fix
Ensure dictionary bytes are identical for corresponding compression and decompression operations. Store and manage dictionaries carefully.
affects: 0.5.0 and later
Errors
Common errors & fixes
AttributeError: module 'brotli' has no attribute 'error'
This error often occurs due to a conflict between `brotlipy` (which imports its functionality under the `brotli` namespace) and the official `brotli` Python package (Google's native bindings), or a corrupted `brotlipy` installation where the `error` attribute, aliased for compatibility, is missing.
fix
To resolve this, ensure only one Brotli-related package is installed. If you intend to use `brotlipy` (version 0.7.0), try reinstalling it (`pip uninstall brotlipy && pip install brotlipy`). If you require the newer `brotlicffi` or Google's `brotli` package, uninstall `brotlipy` and install the desired alternative (`pip uninstall brotlipy && pip install brotlicffi`) and adjust imports from `import brotli` to `import brotlicffi`.
Failed building wheel for brotlipy
`brotlipy` relies on CFFI to bind to the native Brotli library, requiring a C compiler, Python header files, and `libffi` during installation, especially on Linux systems or if a pre-built wheel for your specific environment is not available.
fix
Install the necessary build dependencies for your operating system. For Debian/Ubuntu, use `sudo apt-get install build-essential python3-dev libffi-dev` (adjust `python3-dev` for your Python version). For Red Hat/Fedora, use `sudo yum install gcc libffi-devel python3-devel`. After installing dependencies, try `pip install brotlipy` again.
ModuleNotFoundError: No module named '_brotli'
This error indicates that the underlying CFFI-generated native module for `brotli` (provided by `brotlipy`) could not be found or loaded, often due to an incomplete or corrupted installation of `brotlipy` itself.
fix
First, ensure `brotlipy` is uninstalled (`pip uninstall brotlipy`). Then, try reinstalling it, ensuring all build dependencies are met (refer to the 'Failed building wheel' fix if installation issues persist). If using a virtual environment, make sure it's activated. As `brotlipy` is archived, consider migrating to `brotlicffi` (`pip install brotlicffi`) as its active successor.
ModuleNotFoundError: No module named '_cffi_backend'
`brotlipy` is built using CFFI, and this error signifies that the `cffi` library itself, or its crucial `_cffi_backend` native module, is either not correctly installed, corrupted, or cannot be located by your Python environment.
fix
Reinstall the `cffi` package explicitly: `pip uninstall cffi && pip install cffi`. If the problem persists, ensure your Python installation is healthy and that you have a C compiler and development headers installed, as `cffi` also requires compilation for its native components. In virtual environments, ensure `cffi` is installed within that environment.
Upgrade
Version history
0.7.0latest on PyPI · released May 30, 2017
Audit
Dependencies
cffirequiredRuntime dependency for the CFFI bindings.
Agent activity
16 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
brotlipy — pip install brotlipy · libregistry