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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
parse_mime_type
✓ from mimeparse import parse_mime_type
parse_media_range
✓ from mimeparse import parse_media_range
quality
✓ from mimeparse import quality
quality_parsed
✓ from mimeparse import quality_parsed
best_match
✓ from mimeparse import best_match
This quickstart demonstrates how to use `best_match` to select the most suitable MIME type based on an `Accept` header and available types, and `parse_mime_type` to break down a MIME type into its components.
from mimeparse import best_match
supported_types = ['application/json', 'text/html', 'image/jpeg', 'text/plain']
accept_header = 'text/*;q=0.5, application/json;q=1.0, */*;q=0.8'
match = best_match(supported_types, accept_header)
print(f"Best match for '{accept_header}' from supported types: {match}")
# Example with parsing a specific mime type
from mimeparse import parse_mime_type
mime_type = 'application/json; charset=utf-8'
parsed_type = parse_mime_type(mime_type)
print(f"Parsed '{mime_type}': {parsed_type}")
Debug
Known issues
breakingVersion 2.0.0 dropped official support for Python 3.7. Users on Python 3.7 or older must remain on python-mimeparse 1.x.fixUpgrade to Python 3.8+ or pin python-mimeparse to a 1.x version.
affects: <2.0.0
gotchaThere is an older, unmaintained package named `mimeparse` on PyPI (last updated 2013). Ensure you install `python-mimeparse` to get the actively maintained version.fixAlways use `pip install python-mimeparse` instead of `pip install mimeparse`.
affects: All
gotchaBehavior related to `q` (quality) parameters in media ranges (e.g., in `Accept` headers) was significantly enhanced in version 1.6.0. Upgrading from versions prior to 1.6.0 might result in different `best_match` or `quality` outcomes for complex media range comparisons due to more accurate RFC compliance.fixReview logic using `best_match` or `quality` if upgrading from pre-1.6.0 versions to account for improved q-value handling.
affects: <1.6.0
breakingVersion 2.0.0 internally addressed the deprecation and removal of the `cgi` module in Python 3.13 by vendoring necessary functions. While this prevents a future runtime error for users, it represents a significant internal compatibility overhaul for Python versions beyond 3.11.fixNo direct user fix needed, but be aware that older versions of `python-mimeparse` would not be compatible with Python 3.13+.
affects: All Python 3.13+
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'mimeparse'
The `python-mimeparse` package has not been installed in the current Python environment.
fixpip install python-mimeparse
TypeError: 'str' object is not iterable
The first argument to `mimeparse.best_match` (acceptable MIME types) must be an iterable (e.g., a list or tuple), but a single string was provided.
fixmimeparse.best_match(['image/*', 'text/*'], 'image/png')
AttributeError: module 'mimeparse' has no attribute 'parseMimeType'
The function name `parseMimeType` uses camelCase, but the correct function name in the `mimeparse` library is `parse_mime_type` (snake_case).
fixmimeparse.parse_mime_type('text/html') TypeError: expected string or bytes-like object
The `mimeparse.parse_mime_type` function expects its argument to be a string representing a MIME type, but a non-string type (e.g., None, int) was provided.
fixmimeparse.parse_mime_type('application/json') Upgrade
Version history
2.0.0latest on PyPI · released Aug 25, 2024
Audit
Dependencies
No dependency data recorded yet.