Registry / serialization / hexbytes

hexbytes

JSON →
library2.0.0pypypi✓ verified 24d ago

HexBytes is a thin wrapper around the Python built-in `bytes` class (version 1.3.1). It extends `bytes` by accepting a wider range of initialization types including `bool`, `int`, `str`, `bytearray`, and `memoryview`. Key features include a 0x-prefixed console representation (`__repr__`) and a `to_0x_hex()` method for consistent 0x-prefixed hexadecimal string output. The library is actively maintained with regular releases.

pip install hexbytes
INSTALL
IMPORT
SIG · HEXBYTES
H
hexbytes
serializationpythonv2.0.0
Install
1.6s avg
Import
109ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.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
installs and imports cleanly · install 0.0s · import 0.110s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.108s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

HexBytes
from hexbytes import HexBytes

Initialize HexBytes from various types and retrieve different representations.

from hexbytes import HexBytes # Convert from bytes to a prettier representation hb_from_bytes = HexBytes(b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;") print(f"From bytes: {hb_from_bytes}") # HexBytes accepts hex string representation (ignoring case and 0x prefixes) hb_from_hex_str = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B') print(f"From hex string: {hb_from_hex_str}") # Get the 0x-prefixed hex string hex_string_output = hb_from_hex_str.to_0x_hex() print(f"Using to_0x_hex(): {hex_string_output}") # Cast back to basic bytes type original_bytes = bytes(hb_from_hex_str) print(f"Cast back to bytes: {original_bytes}")
Debug
Known issues
breakingHexBytes has dropped support for older Python versions. Versions prior to 0.2.0 dropped Python 3.5, versions prior to 0.3.0 dropped Python 3.6, and current 1.x series (since 1.0.0) requires Python >= 3.8. Attempting to install on unsupported versions will fail or lead to unexpected behavior.
fix
Ensure your Python environment is 3.8 or newer for HexBytes 1.x. For older Python versions, use an older HexBytes release (e.g., <0.3.0 for Python 3.7 compatibility).
affects: <1.0.0 (Python 3.5, 3.6), >=1.0.0 (Python <3.8)
gotchaWhile HexBytes' `__repr__` method provides a convenient 0x-prefixed hex string for console output, it does not override the standard `bytes.hex()` or `__str__` methods. Calling `str(my_hexbytes)` or `my_hexbytes.hex()` will revert to the default `bytes` behavior (e.g., `b'\x01\x02'` or `'0102'`).
fix
To consistently get a 0x-prefixed hex string, always use the `my_hexbytes.to_0x_hex()` method.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'hexbytes'
The 'hexbytes' library is not installed in the Python environment.
fix
Install the 'hexbytes' library using pip: 'pip install hexbytes'.
AttributeError: 'HexBytes' object has no attribute 'hex'
The 'hex' method in 'HexBytes' returns a hexadecimal string without the '0x' prefix.
fix
Use the 'to_0x_hex()' method to get a '0x'-prefixed hexadecimal string: 'hb.to_0x_hex()'.
TypeError: 'HexBytes' object is not callable
Attempting to call a 'HexBytes' object as a function, which is not supported.
fix
Ensure that 'HexBytes' instances are not used as callable functions; use methods like 'to_0x_hex()' instead.
ValueError: invalid literal for int() with base 16: '0x'
Passing a '0x'-prefixed string to 'HexBytes' without removing the prefix.
fix
Initialize 'HexBytes' with a hexadecimal string without the '0x' prefix: 'hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B')'.
TypeError: 'HexBytes' object is not subscriptable
Attempting to access elements of a 'HexBytes' object using indexing, which is not supported.
fix
Convert 'HexBytes' to a 'bytes' object before indexing: 'bytes(hb)[0]'.
Upgrade
Version history
2.0.0latest on PyPI · released Aug 21, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
hexbytes — pip install hexbytes · libregistry