Registry / serialization / python-snappy

python-snappy

JSON →
library0.7.3pypypi✓ verified 27d ago

python-snappy is a Python library that provides a high-performance wrapper for Google's Snappy compression library. It's designed for fast data compression and decompression, suitable for applications like data transfer, storage, and database indexing. The current version is 0.7.3. Releases are made periodically to maintain compatibility with newer Python versions and address underlying library changes.

pip install python-snappy
INSTALL
IMPORT
SIG · PYTHON-SNAPPY
P
python-snappy
serializationpythonv0.7.3
Install
1.9s avg
Import
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.3 · 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
installs and imports cleanly · install 0.0s · import 0.000s · 22.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.000s · 23MB
20MB installed
● package 20MB
Code
Verified usage

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

snappy
import snappy
compress
snappy.compress
Called as a function from the top-level 'snappy' module.
decompress
snappy.decompress
Called as a function from the top-level 'snappy' module.
StreamCompressor
snappy.StreamCompressor
Used for incremental compression of data streams.
StreamDecompressor
snappy.StreamDecompressor
Used for incremental decompression of data streams.

This quickstart demonstrates basic one-shot compression/decompression and the use of `StreamCompressor` and `StreamDecompressor` for handling larger data in chunks. Ensure the underlying Snappy C library is installed first.

import snappy # Basic compression and decompression original_data = b"This is some data to be compressed using snappy!" * 100 compressed_data = snappy.compress(original_data) decompressed_data = snappy.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 and decompression compressor = snappy.StreamCompressor() compressed_stream_chunks = [] # Simulate sending data in chunks for chunk in [b'part one', b'part two', b'part three']: compressed_stream_chunks.append(compressor.compress(chunk)) compressed_stream_chunks.append(compressor.flush()) # Important to flush remaining data full_compressed_stream = b''.join(compressed_stream_chunks) decompressor = snappy.StreamDecompressor() decompressed_stream_chunks = [] # Simulate receiving and decompressing data in chunks for chunk in [full_compressed_stream[:15], full_compressed_stream[15:]]: decompressed_stream_chunks.append(decompressor.decompress(chunk)) final_decompressed_stream = b''.join(decompressed_stream_chunks) print(f"Stream decompressed data: {final_decompressed_stream}") print(f"Stream decompressed data matches original: {final_decompressed_stream == b'part onepart twopart three'}")
Debug
Known issues
gotchaThe `python-snappy` library is a wrapper around the Snappy C++ library. You must have the underlying `snappy` library installed on your system before `pip install python-snappy` will succeed.
fix
Install `libsnappy-dev` (Debian/Ubuntu), `snappy-devel` (RHEL/Fedora), or `snappy` (macOS via Homebrew) using your system's package manager before installing the Python package.
affects: All versions
gotcha`python-snappy` functions (like `snappy.compress` and `snappy.decompress`) only operate on `bytes` objects. Passing a `str` object will result in a `TypeError`.
fix
Ensure all data intended for compression or decompression is of type `bytes`. Use `my_string.encode('utf-8')` to convert strings to bytes.
affects: All versions
gotchaWhen using `StreamCompressor`, it's crucial to call `compressor.flush()` at the end of the data stream to ensure all buffered data is emitted. Failing to do so can lead to incomplete or corrupted compressed output.
fix
Always append the result of `compressor.flush()` to your compressed data stream after all chunks have been processed by `compressor.compress()`.
affects: All versions
gotchaThe latest versions of `python-snappy` (0.7.0+) explicitly require Python 3.7 or newer. Older Python 3 versions (e.g., 3.6) or Python 2 are no longer supported.
fix
Ensure your environment is running Python 3.7 or newer. If you must use an older Python version, you might need to pin `python-snappy` to an older compatible version (e.g., `<0.7.0`).
affects: 0.7.0+
Errors
Common errors & fixes
fatal error: snappy-c.h: No such file or directory
The C/C++ development headers for the Snappy library are not installed on your system, which are required to compile the `python-snappy` package from source.
fix
Install the Snappy development headers using your system's package manager (e.g., `sudo apt-get install libsnappy-dev` on Debian/Ubuntu, `sudo yum install snappy-devel` on Fedora/RHEL, or `brew install snappy` on macOS). Then retry `pip install python-snappy`.
ModuleNotFoundError: No module named 'snappy'
The `python-snappy` library has not been installed in your current Python environment or is not accessible.
fix
Install the library using pip: `pip install python-snappy`.
AttributeError: 'bytes' object has no attribute 'decode'
You are attempting to call the `.decode()` method directly on the compressed bytes returned by `snappy.compress()`, but compressed data must first be decompressed before it can be decoded into a string.
fix
First decompress the data using `snappy.decompress()`, then decode the resulting bytes if you expect a string. Example: `original_bytes = snappy.decompress(compressed_data); original_string = original_bytes.decode('utf-8')`.
ModuleNotFoundError: No module named 'python_snappy'
The `python-snappy` package exposes its functionality under the module name `snappy`, not `python_snappy` (which is the package name you install via pip).
fix
Import the library using `import snappy` or `from snappy import compress, decompress`.
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools"
On Windows, compiling Python packages with C/C++ extensions (like `python-snappy`) requires the Microsoft Visual C++ build tools, which are often missing or outdated.
fix
Download and install the "Build Tools for Visual Studio" from `https://visualstudio.microsoft.com/visual-cpp-build-tools/`, ensuring you select the "Desktop development with C++" workload during installation. Then retry `pip install python-snappy`.
Upgrade
Version history
0.7.3latest on PyPI · released Aug 29, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
Resources
python-snappy — pip install python-snappy · libregistry