py-multibase
py-multibase is a Python library that provides an implementation of the Multibase protocol. Multibase is designed to distinguish base encodings (like base64, base58btc, etc.) and other simple string encodings, ensuring compatibility across different systems by making the encoding self-describing. The library is currently at version 2.0.0 and is actively maintained, facilitating encoding and decoding of data with various Multibase formats.
Common errors
-
ModuleNotFoundError: No module named 'multibase'
cause The `py-multibase` library is either not installed or not available in the current Python environment.fixInstall the library using pip: `pip install py-multibase` -
TypeError: a bytes-like object is required, not 'str'
cause You are passing a Python string to a function (e.g., `multibase.encode`) that expects `bytes` or a bytes-like object, without explicitly encoding the string first.fixConvert your string to bytes before passing it to the function: `my_string.encode('utf-8')`. Example: `encode('base64', 'hello world'.encode('utf-8'))` or ensure the function handles string input if it's designed to. -
ValueError: Unknown multibase encoding: 'some_invalid_base'
cause The specified multibase encoding (e.g., 'some_invalid_base') is not recognized or supported by the library.fixUse a valid multibase name as defined in the Multibase specification and supported by `py-multibase`. You can use `multibase.list_encodings()` to see all supported options.
Warnings
- breaking Version 2.0.0 and higher of `py-multibase` requires Python 3.10 or newer. Users on older Python versions (e.g., 3.9 or earlier) must remain on `py-multibase` 1.x or upgrade their Python interpreter.
- gotcha Not all multibase encodings from the official specification may be fully implemented in `py-multibase` 2.0.0. GitHub issues indicate ongoing work to achieve 100% compatibility with reference implementations. Verify support for specific or less common encodings if they are critical to your application.
- gotcha The `encode` function can accept both string and bytes input, but `decode` always returns `bytes`. Attempting to treat `bytes` output directly as a Python `str` without explicit decoding (e.g., `.decode('utf-8')`) will lead to `TypeError` or unexpected behavior.
Install
-
pip install py-multibase
Imports
- encode, decode
from multibase import encode, decode
- Encoder, Decoder
from multibase import Encoder, Decoder
- get_encoding_info, list_encodings
from multibase import get_encoding_info, list_encodings
Quickstart
from multibase import encode, decode, list_encodings
# Encode a string to base58btc
data_to_encode = 'hello world'
encoded_base58 = encode('base58btc', data_to_encode)
print(f"Encoded (base58btc): {encoded_base58}")
# Encode a string to base64
encoded_base64 = encode('base64', data_to_encode)
print(f"Encoded (base64): {encoded_base64}")
# Decode a multibase string
decoded_data = decode(encoded_base64)
print(f"Decoded: {decoded_data.decode('utf-8')}")
# Using reusable Encoder/Decoder classes
from multibase import Encoder, Decoder
base64_encoder = Encoder('base64')
encoded_reusable = base64_encoder.encode(b'reusable data')
print(f"Encoded (reusable base64): {encoded_reusable}")
decoder = Decoder()
decoded_reusable = decoder.decode(encoded_reusable)
print(f"Decoded (reusable): {decoded_reusable.decode('utf-8')}")
# List available encodings
# print(list_encodings())