chardet: Universal Character Encoding Detector
raw JSON → 7.4.0.post1 verified Tue May 12 auth: no python install: verified quickstart: verified
chardet is a Python library that detects the character encoding of byte strings, providing the detected encoding, confidence score, and language. The current version is 7.4.0.post1, released on March 14, 2026. It is actively maintained with regular updates, focusing on improving accuracy and performance. The library requires Python 3.10 or higher and has zero runtime dependencies, making it suitable for various Python environments, including PyPy.
pip install chardet Common errors
error ModuleNotFoundError: No module named 'chardet' ↓
cause The `chardet` library is not installed in the Python environment being used, or the Python interpreter cannot find it due to incorrect path configurations or virtual environment issues.
fix
Install the
chardet library using pip: pip install chardet (or pip3 install chardet for Python 3 specific installations). error TypeError: Expected object of type bytes or bytearray, got: <class 'str'> ↓
cause The `chardet.detect()` function, or other functions within the library, expects a byte string (`bytes` or `bytearray`) as input, but a regular Python string (`str`) was provided.
fix
Convert the string input to bytes using an appropriate encoding (e.g.,
some_string.encode('utf-8')) before passing it to chardet.detect(). Example: chardet.detect(your_string.encode('utf-8')). If reading from a file, open it in binary read mode ('rb'). error chardet returns None for encoding ↓
cause The `chardet.detect()` function returns `None` for the 'encoding' key when the provided data appears to be binary rather than text, or contains too many null bytes or control characters that prevent a confident text encoding detection.
fix
Ensure the input data is indeed text-like. If it's genuinely binary,
chardet cannot detect a text encoding. If it is text, providing more data to chardet.detect() or using chardet.detect_all() to see other candidates might help. Warnings
breaking chardet 7.0 is a ground-up, MIT-licensed rewrite — same package name, same public API — drop-in replacement for chardet 5.x/6.x. Python 3.10+, zero runtime dependencies, works on PyPy. ↓
fix Update to chardet 7.0 or later to ensure compatibility and benefit from improved performance and accuracy.
deprecated chardet 7.0.0 introduced a new dense zlib-compressed model format (v2) that significantly reduces cold start times. Ensure your environment supports this format for optimal performance. ↓
fix Verify that your environment is compatible with the new model format to take advantage of the performance improvements.
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.07s 18.9M
3.10 slim (glibc) - - 0.05s 20M
3.11 alpine (musl) - - 0.13s 20.8M
3.11 slim (glibc) - - 0.10s 22M
3.12 alpine (musl) - - 0.12s 12.7M
3.12 slim (glibc) - - 0.12s 14M
3.13 alpine (musl) - - 0.10s 12.3M
3.13 slim (glibc) - - 0.09s 14M
3.9 alpine (musl) - - 0.05s 19.0M
3.9 slim (glibc) - - 0.05s 20M
Imports
- detect
from chardet import detect
Quickstart verified last tested: 2026-04-23
import chardet
# Detect encoding of a byte string
result = chardet.detect(b'Hello, world!')
print(result)
# Output: {'encoding': 'ascii', 'confidence': 1.0, 'language': 'en'}